2012-03-18 92 views
23

我試圖在Google App Engine上使用真棒請求庫。我找到了一個urllib3的補丁,它依賴於與App Engine兼容的請求。 https://github.com/shazow/urllib3/issues/61在Google App Engine中使用請求python庫

我可以成功

import requests 

但隨後

response = requests.get('someurl') 

失敗,出現以下回溯。這是怎麼回事?

Traceback (most recent call last): 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/admin/__init__.py", line 317, in post 
    exec(compiled_code, globals()) 
    File "<string>", line 6, in <module> 
    File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/api.py", line 52, in get 
    return request('get', url, **kwargs) 
    File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/api.py", line 40, in request 
    return s.request(method=method, url=url, **kwargs) 
    File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/sessions.py", line 208, in request 
    r.send(prefetch=prefetch) 
    File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/models.py", line 458, in send 
    self.auth = get_netrc_auth(url) 
    File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/utils.py", line 43, in get_netrc_auth 
    for loc in locations: 
    File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/utils.py", line 40, in <genexpr> 
    locations = (os.path.expanduser('~/{0}'.format(f)) for f in NETRC_FILES) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 260, in expanduser 
    userhome = pwd.getpwuid(os.getuid()).pw_dir 
AttributeError: 'module' object has no attribute 'getuid' 
+0

可能重複[要求:HTTP人類 - python-requests.org在谷歌應用程序引擎(http://stackoverflow.com/questions/9604799/requests-http-for-humans-python-requests- org-on-google-app-engine) – 2012-04-07 18:06:48

回答

13

如前所述,master branch of standalone urllib3理應支持的AppEngine現在(我會做一個適當的PyPI發佈一次有人證實了這一事實),但要求還不支持AppEngine上,因爲它使有關各種文件系統的東西加載配置文件假設這在AppEngine上不存在。具體來說,您遇到的錯誤與加載~/.netrc配置文件有關。

參見Issue #493

對於它的價值,在urllib3相當於將是:

import urllib3 
http = urllib3.PoolManager() 
response = http.request('GET', 'someurl') 

更新:urllib3 v1.3昨天,包括AppEngine上支持釋放。

+0

非常感謝,shazow。我正在App Engine上成功使用修補後的urllib3。猜猜我們必須看到抽象出一些這種低級文件系統的東西是多麼困難。 – rd108 2012-03-19 00:24:28

9

對谷歌的AppEngine生產工作(1.9.18版)requests版本2.3.0(僅!)(但不是在SDK),如果你啓用了結算,使插座的支持。在AppEngine上SDK

請求失敗,所有的https://請求:

ConnectionError: ('Connection aborted.', error(13, 'Permission denied')) 

請求版本2.4.1失敗:

File "distlib/requests/adapters.py", line 407, in send 
    raise ConnectionError(err, request=request) 
    ConnectionError: ('Connection aborted.', error(13, 'Permission denied')) 

請求版本2.5.1失敗:

File "distlib/requests/adapters.py", line 415, in send 
    raise ConnectionError(err, request=request) 
    ConnectionError: ('Connection aborted.', error(13, 'Permission denied')) 

插座支持信息:https://cloud.google.com/appengine/docs/python/sockets/

PS:如果您打算在GAE上使用請求,請以非常痛苦的方式替換awsome。

另請參見:Can Python Requests library be used on Google App Engine?

+0

但可以使用[urlfetch](https://cloud.google.com/appengine/docs/python/urlfetch/)。不知道是否有可能使'請求'使用它。 – wlnirvana 2015-02-23 11:34:10

+0

否。請求的維護者不支持appengine。在大多數情況下,您可以很容易地將來自第三方庫的調用替換爲/使用urlfetch更改請求,或者直接通過兼容調用urlfetch來請求請求。我們有一個名爲fake_requests.py的模塊來模擬請求。 – cat 2015-03-07 10:39:08

+0

版本2.3.0確實爲我做了詭計。請參閱http://stackoverflow.com/questions/29301863/google-app-engine-and-human-api-python-lib – 2015-03-28 17:59:41

相關問題