【问题标题】:Coverting POST from requests to GAE urlfetch将 POST 从请求转换为 GAE urlfetch
【发布时间】:2015-04-16 15:58:45
【问题描述】:

我正在使用 PayPal 付款。以下是它如何与requests 一起正常工作:

res = requests.post(get_payment_info_url, headers=headers, data=params)
res_data = res.json()

但是当我尝试使用 urlfetch 执行相同的请求时,它给了我一个错误(来自 PayPal 的 200 响应,但付款失败):

res = urlfetch.fetch(url=make_payment_url, payload=params, method=urlfetch.POST, headers=headers)
res_data = json.loads(res)

{u'responseEnvelope': {u'timestamp': u'2015-02-15T23:21:52.729-08:00', u'ack': u'Failure', u'build': u'15089777', u'correlationId': u'e202988541fde'}, 
u'error': [{u'domain': u'PLATFORM', u'message': u'Invalid request: {0}', u'severity': u'Error', u'subdomain': 
u'Application', u'category': u'Application', u'errorId': u'580001'}]}

似乎谷歌正在剥离标题或什么?如果 Google 这样做,我将如何提出此请求?

最后,是否有任何理由使用 urlfetch 而不是 requests(我已在本地导入到我的 GAE 项目中?请求似乎更容易使用且“友好”。

【问题讨论】:

    标签: python multithreading google-app-engine python-requests


    【解决方案1】:

    看看https://github.com/paypal/PayPal-Python-SDK,我设法轻松地修补了这个库,以便与 GAE 一起使用,如下所述: https://github.com/paypal/PayPal-Python-SDK/issues/66

    Requests 适用于 GAE,但仅适用于 2.3.0(!) 版本

    在 Google Appengine(版本 1.9.17)requests版本 2.3.0(仅限!)生产中(但不适用于 SDK)(如果您有计费) enabled,启用套接字支持。

    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'))
    

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

    【讨论】:

      【解决方案2】:

      为此,需要对有效负载进行 urlencoded。这是有效的:

      res2 = urlfetch.fetch(
                       url,
                       headers=headers,
                       method='POST',
                       payload=urllib.urlencode(params)
                     )
      res2_data = json.loads(res2.content)
      

      【讨论】:

      • 谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2019-10-11
      • 1970-01-01
      相关资源
      最近更新 更多