在Google App Engine中使用Requests python库

时间:2012-03-18 21:57:29

标签: google-app-engine python-requests urllib3

我正在尝试在Google App Engine上使用令人敬畏的Requests库。我找到了urllib3的补丁,它请求依赖,与App Engine兼容。 https://github.com/shazow/urllib3/issues/61

我可以成功

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'

3 个答案:

答案 0 :(得分:14)

如上所述,master branch of standalone urllib3据说现在支持AppEngine(一旦有人确认这个事实,我会做一个正确的PyPI发布),但Requests还不支持AppEngine,因为它假设各种文件系统的东西加载配置文件在AppEngine上不存在。具体而言,您遇到的错误与加载~/.netrc配置文件有关。

请参阅Issue #493

对于它的价值,urllib3中的等价物将是:

import urllib3
http = urllib3.PoolManager()
response = http.request('GET', 'someurl')
昨天发布了

更新: urllib3 v1.3,其中包括AppEngine支持。

答案 1 :(得分:9)

在Google Appengine上(版本1.9.18)requests 版本2.3.0(仅限!) 中工作(但不在SDK上)已启用计费,这将启用套接字支持。

Appengine SDK上的

请求因所有https:// requests:

而失败
  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?

答案 2 :(得分:9)

您可以在requests-toolbelt的帮助下在Google App Engine上使用最新版本的请求。这会将请求配置为使用urllib3对App Engine的URLFetch服务的底层支持。