在 Google App Engine 中使用 Requests python 库 [英] Using the Requests python library in Google App Engine

查看:29
本文介绍了在 Google App Engine 中使用 Requests python 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use the awesome Requests library on Google App Engine. I found a patch for urllib3, which requests relies on, that is compatible with App Engine. https://github.com/shazow/urllib3/issues/61

我可以成功

import requests

然后

response = requests.get('someurl')

因以下回溯而失败.怎么回事?

fails with the following traceback. What's going on?

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'

推荐答案

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

As mentioned, master branch of standalone urllib3 supposedly supports AppEngine now (I'll do a proper PyPI release once someone confirms this fact), but Requests does not yet support AppEngine since it makes assumption about various filesystem things to load configuration files which don't exist on AppEngine. Specifically the error you ran into has to do with loading the ~/.netrc configuration file.

请参阅问题#493.

就其价值而言,urllib3 中的等效项是:

For what it's worth, the equivalent in urllib3 would be:

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

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

Update: urllib3 v1.3 was released yesterday which includes AppEngine support.

这篇关于在 Google App Engine 中使用 Requests python 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆