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

2024-05-20 00:05:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python编写了一个项目,现在正在迁移到googleappengine。出现的问题是在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上运行代码时,第一个请求中的cookies丢失。也就是说,r1.cookies只是一个空的cookiejar。同样的代码在我的django服务器上运行得很好,其中cookie应该包含asp.net会话id

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

在GAE上打印输出:

^{pr2}$

在Django上打印输出:

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

有人知道问题出在哪里吗?盖伊是在窃取饼干信息吗?我也愿意接受任何关于另一种检索页面方法的建议,我只是发现请求模块比我找到的其他方法更容易。在


Tags: the代码importcomidurlgetresponse
2条回答

我试过urlphetch它似乎显示了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测试。在

相关问题 更多 >