在Google App Engine中使用Requests python库

问题描述 投票:24回答:3

我正在尝试在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'
google-app-engine python-requests urllib3
3个回答
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支持。


9
投票

在Google 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?


9
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.