简体   繁体   English

Google Appengine:要求替代

[英]Google Appengine: Requests Alternative

I have a non-GAE application/request-handler that uses the Python requests module in a to post an uploaded imaged via a POST request, as binary: 我有一个非GAE应用程序/请求处理程序,该应用程序在中使用Python请求模块以二进制形式发布通过POST请求上传的图片:

headers = {"MyAuth" : "xyz"}
r = requests.post(base_uri, data=open('0.jpg')), headers=headers)

The user uploads an image, the uploaded image is saved locally, opened for reading, then sent to a remote classifier pipeline via post request - this returns some JSON regarding the image features, which can then be returned to the user. 用户上传图像,上传的图像保存在本地,打开以供阅读,然后通过发布请求发送到远程分类器管道-这返回有关图像特征的JSON,然后可以将其返回给用户。

I need to implement this behaviour in a GAE app, but know that GAE has no traditional file system, so I will have to use StringIO : 我需要在GAE应用程序中实现此行为,但要知道GAE没有传统的文件系统,因此我将不得不使用StringIO

data = ... #some jpg => str
headers = {"MyAuth" : "xyz"}
r = requests.post(base_uri, data=StringIO.StringIO(data), headers=headers)

How could I completely replace the requests module in this example in a GAE friendly way? 如何以GAE友好的方式完全替换此示例中的请求模块?

Many thanks. 非常感谢。

Commonly used module for making HTTP requests on app engine is urlfetch , it is available in the default runtime via google.appengine.api.urlfetch . 在应用引擎上发出HTTP请求的常用模块是urlfetch ,它可以在默认运行时通过google.appengine.api.urlfetch Supposedly urllib2 and/or urllib3 are also options, but I have not used those myself so I can't say for sure. 据说urllib2和/或urllib3也是选项,但是我自己没有使用过,所以不能确定。

You can also install requests in your app engine directory and upload it with the project, but I find that a bit of a hassle, since requests has its own dependencies that you will need to include as well. 您还可以将请求安装到App Engine目录中,然后将其与项目一起上传,但是我发现这有点麻烦,因为请求具有自己的依赖关系,您也需要包括这些依赖关系。

Also see Using the Requests python library in Google App Engine 另请参阅在Google App Engine中使用请求python库

Although probably not the best solution to this problem, I managed to get requests 2.3.0 to work in the GAE project with: 尽管可能不是解决此问题的最佳解决方案,但我设法通过以下方式获得了请求2.3.0在GAE项目中的工作:

pip install --target myproject/externals/ requests==2.3.0

I can now use requests as I would normally. 现在,我可以像往常一样使用请求了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM