在Google App Engine中使用Requests python库

rd1*_*108 24 google-app-engine urllib3 python-requests

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

我能成功

import requests
Run Code Online (Sandbox Code Playgroud)

但是之后

response = requests.get('someurl')
Run Code Online (Sandbox Code Playgroud)

以下回溯失败.这是怎么回事?

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'
Run Code Online (Sandbox Code Playgroud)

sha*_*zow 14

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

问题#493.

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

import urllib3
http = urllib3.PoolManager()
response = http.request('GET', 'someurl')
Run Code Online (Sandbox Code Playgroud)

更新: urllib3 v1.3昨天发布,其中包括AppEngine支持.


cat*_*cat 9

在谷歌的AppEngine(1.9.18版本)请求 版本2.3.0(仅!)工作在生产中(而不是在SDK),如果你有计费启用,这使得插座的支持.

Appengine SDK上的请求因所有https://请求而失败:

  ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))
Run Code Online (Sandbox Code Playgroud)

请求版本2.4.1失败:

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

请求版本2.5.1失败:

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

有关套接字支持的信息:https://cloud.google.com/appengine/docs/python/sockets/

PS:如果您打算在GAE上使用请求,请用非常痛苦的方式替换awsome.

另请参阅:可以在Google App Engine上使用Python请求库吗?


Jon*_*ott 9

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