2012-03-07 69 views

回答

25

是的。在Google Appengine上(版本1.9.18)requests版本2.3.0在製作中工作(但不在SDK上)如果您啓用了計費功能,則支持套接字。

更新:截至2017年1月31日請求正在生產中使用版本2.9.1進行生產。然而,這不是與谷歌雲SDK 141.0.0工作的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')) 

請求版本2.3.0在生產環境中工作,但可能會導致Debian在SDK中刪除SSLv3支持(請求2.3.0自帶現在過時的urllib3)。作爲解決方法,可以刪除請求的urllib3副本源中包含PROTOCOL_SSLv3的行。

'module' object has no attribute 'PROTOCOL_SSLv3' 

信息上的插座的支持:https://cloud.google.com/appengine/docs/python/sockets/

+3

您可以通過加載SSL庫來解決https://請求http://stackoverflow.com/a/34135758/172322 – Yahel 2015-12-07 14:20:38

+0

對https網站的請求即使在我未啓用結算時也能正常工作 - 爲什麼? – 2015-12-12 18:02:09

+0

我剛剛發現,只要套接字庫沒有被實際使用(通過請求庫),如果賬單沒有被啓用(即未使用的導入和未使用的代碼很好),App Engine將不會引發異常。這是因爲我只做了簡單的GET請求。實際上在底層代碼中調用的是App Engine的URL獲取庫,當我檢查使用的配額(在我的使用情況下,使用urllib2而不是請求可能是更好的選擇)。此外,它的工作原理不包括我的情況下的SSL庫; App Engine的URL抓取支持https – 2015-12-13 08:02:32

7

還沒有,但希望很快。對GAE的支持正在進行中 - 請參閱問題#498(App Engine修復程序)。

請求使用urllib3然後使用httplib,其中is在GAE上支持作爲urlfetch API的包裝。儘管urllib3使用GAE上不可用的某些模塊,但此用法故意作爲optional,以便在GAE上使用urllib3。

+0

你知道這是否改變了嗎? – clifgray 2013-07-22 20:00:57

+0

我不知道,但你可以從問題的答案問。 – 2013-07-22 20:54:50

+1

它現在工作正常。 – clifgray 2013-07-23 01:12:06

3

不,more recent post,開發人員說他們不支持GAE,因爲它與python太不一樣了。

+0

他們不支持它,但你仍然可以使用庫。只需下載源代碼並直接導入。如果您需要使用套接字來解決'urlfetch()'限制,請使用[套接字](https://developers.google.com/appengine/docs/python/sockets/) – user3058197 2014-07-23 23:11:23

+0

我試過了,它不會導入它。 – 2014-07-24 17:55:25

+0

你有套接字嗎? – user3058197 2014-07-24 19:36:43

25

安裝requests-toolbelt庫: https://github.com/sigmavirus24/requests-toolbelt

App Engine的可能是這樣的:pip install requests-toolbelt -t lib

(參見:https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#installing_a_library

然後添加:

from requests_toolbelt.adapters import appengine 
appengine.monkeypatch() 

在您的main.py或同等產品中。

編輯:該解決方案是目前正式文件的一部分:https://cloud.google.com/appengine/docs/python/issue-requests#issuing_an_http_request (在REQUESTS標籤)

+0

這應該是被接受的答案! – Alex 2017-07-11 19:13:55

+1

僅供參考,在導入任何使用請求的模塊之前,appengine.monkeypatch()行應該在您的導入中。 – 2018-02-12 00:25:39

5

這是目前可能的,我懂了工作使用appengine_config解決方法的結合。py:

# Step 1: first add requests and requests-toolbelt to your requirements.txt (or however you install them via pip) 
# Step 2: in appengine_config.py add the following snippet: 

# see https://cloud.google.com/appengine/docs/python/issue-requests#issuing_an_http_request 
import requests 
import requests_toolbelt.adapters.appengine 

# Use the App Engine Requests adapter. This makes sure that Requests uses 
# URLFetch. 
requests_toolbelt.adapters.appengine.monkeypatch() 

# also monkey patch platform.platform() from https://code.google.com/p/googleappengine/issues/detail?id=12982 
import platform 

def patch(module): 
    def decorate(func): 
     setattr(module, func.func_name, func) 
     return func 
    return decorate 


@patch(platform) 
def platform(): 
    return 'AppEngine' 
+0

謝謝,除了'def platform()' – vkb 2016-11-28 21:32:40

+0

謝謝!代碼現在更新超過 – jcjones1515 2016-11-30 14:33:21

+0

這應該是公認的答案,因爲現在谷歌寫入到他們的文檔。 requests_toolbelt猴子補丁的作品。 – 2017-12-27 06:16:28

相關問題