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

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

问题描述

我试图在Google App Engine上使用真棒请求库。我找到了一个urllib3的补丁,它依赖于与App Engine兼容的请求。 https://github.com/shazow/urllib3/issues/61



我可以成功

 进口请求

但是然后

  response = requests.get('someurl') 

失败,并显示以下追踪。发生了什么?

  Traceback(最近一次调用的最后一次):
文件/Applications/GoogleAppEngineLauncher.app/Contents /Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/admin/__init__.py,第317行,后
exec(compiled_code,globals())
文件< string>,第6行,位于< module>
文件/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/api.py,第52行,获取
返回请求('get',url, ** kwargs)
文件/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/api.py,第40行,请求
return s.request( method = method,url = url,** kwargs)
文件/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/sessions.py,第208行,请求
r.send(prefetch = prefetch)
文件/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/models.py,第458行,发送
self.auth = get_netrc_auth(url)
文件/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/utils.py,第43行,在get_netrc_auth
用于loc位置:
文件/ Users / Rohan / Dropbox / MuktiTechnologiesINC / M uktiTechnologies / GAE / humanmictweet / GAE / libraries / requests / utils.py,第40行,在< genexpr>
locations =(os.path.expanduser('〜/ {0}'.format(f))for NETRC_FILES)
文件/System/Library/Frameworks/Python.framework/Versions/ 2.7 / lib / python2.7 / posixpath.py,第260行,位于expanduser
userhome = pwd.getpwuid(os.getuid())。pw_dir
AttributeError:'module'object has no attribute' getuid'


解决方案

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



请参阅 Issue#493

在urllib3中相当于:
$ b $ pre $ import urllib3
http = urllib3.PoolManager ()
response = http.request('GET','someurl')

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


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

I can successfully

import requests

but then

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'

解决方案

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.

See Issue #493.

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

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

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

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

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