Python GAE请求在get请求后不返回cookie



我用python写了一个项目,现在我正在转移到谷歌应用程序引擎。出现的问题是当我在GAE:上运行此代码时

import requests
from google.appengine.api import urlfetch
def retrievePage(url, id):
    response = 'http://online2.citybreak.com/Book/Package/Result.aspx?onlineid=%s' % id
    # Set the timeout to 60 seconds
    urlfetch.set_default_fetch_deadline(60)
    # Send the first request
    r1 = requests.get(url)
    cookies = r1.cookies
    print 'Cookies: %s' % r1.cookies
    # Retrieve the content
    r2 = requests.get(response, cookies=cookies)
    return r2.text

在GAE上运行代码时,第一个请求中的cookie丢失。也就是说,r1.cookies只是一个空的饼干罐。同样的代码在我的django服务器上运行得很好,cookie应该包含asp.net会话id。

我有两个请求的原因是,第一个请求重定向用户,并且只有在会话cookie相同的情况下才会检索到正确的页面。

GAE:上的打印输出

Cookies: <<class 'requests.cookies.RequestsCookieJar'>[]>

Django上的打印输出:

Cookies: <<class 'requests.cookies.RequestsCookieJar'>[<Cookie ASP.NET_SessionId=dhmk1vt3ujgmhhhmbwsclukb for online2.citybreak.com/>]>

有人知道问题出在哪里吗?GAE是否在剥离cookie信息?我也愿意接受任何关于另一种检索页面方式的建议,我只是发现请求模块比我找到的替代方案更容易。

我尝试了urlfetch,它似乎显示了cookie头:

import logging
from google.appengine.api import urlfetch
response = urlfetch.fetch(url)
logging.info(response.headers)

这里有一个PR(补丁):https://github.com/kennethreitz/requests/pull/4044为解决您(以及许多其他人)所面临的问题。在生产和开发中使用GAE进行测试。

最新更新