0

我已经用 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 信息?我也愿意接受有关另一种检索页面的方法的任何建议,我只是发现请求模块比我找到的替代方法更容易。

4

2 回答 2

0

I tried urlfetch it seems to be showing the cookie headers:

import logging
from google.appengine.api import urlfetch

response = urlfetch.fetch(url)
logging.info(response.headers)
于 2013-08-06T14:31:45.060 回答
0

这里有一个 PR(补丁):https ://github.com/kennethreitz/requests/pull/4044用于解决您(和许多其他人)遇到的问题。在生产和开发中通过 GAE 测试。

于 2017-05-21T15:48:05.003 回答