Python 3 httplib2 cookie

发布于 2024-12-03 08:36:42 字数 2629 浏览 9 评论 0原文

已修复,httplin2 无法处理多个 cookie,可以在此处找到补丁:http://code.google.com/p/httplib2/issues/detail?id=146

我正在编写一个 Facebook 登录脚本来获取我的最后通知,但是 我有问题。我已经从 urllib 更改为 httplib2 因为它 对我来说似乎更容易、更快。使用 urllib 我能够得到 正确的 cookie 并在我需要检索时使用它 通知,现在我正在使用 httplib 页面中的示例:

user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN;

rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8' headers = {'用户代理':user_agent,'内容类型': 'application/x-www-form-urlencoded', '接受': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8', '连接':'保持活动','接受编码':'gzip,deflate', '推荐人' : 'http://m.facebook.com/' } 数据= {'电子邮件':'我的用户名','密码':'我的密码', 'post_form_id':'','charset_test':'','版本':1,'ajax':1, 'pxr':1,'gps':1,'登录':'Entrar'} 响应,内容= h.request("http://www.facebook.com/login.php?m=m&refsrc=http://m.facebook.com/&refid=0", "POST", urllib.parse.urlencode(data), headers=headers)

这是我得到的标题:

{'status': '200', 'x-cnection': '关闭', '内容位置':

'http://m.facebook.com/home.php?refsrc=http%3A%2F%2Fm.facebook.com%2F&refid=0&m_sess=***tAm60&_rdr' , '传输编码':'分块','设置cookie': 'datr=******F-OQsTCnbw0Caa;过期=2013 年 9 月 6 日星期五 20:33:26 GMT; 路径=/;域名=.facebook.com; httponly', 'expires': '2000 年 1 月 1 日星期六 00:00:00 GMT', 'x-fb-server': '10.62.188.41', '内容长度': '26702', '-content-encoding': 'gzip', 'pragma': '无缓存', 'cache-control': '私有、无缓存、无存储、必须重新验证', 'date': 'Wed, 2011 年 9 月 7 日 20:33:27 GMT', 'p3p': 'CP="Facebook 不 有 P3P 政策。了解原因:http://fb.me/p3p"', 'content-type': '文本/html; charset=utf-8', 'x-frame-options': '拒绝'}

如果我不使用 h.follow_all_redirects = True 选项,则不会 将我重定向到主页,但设置的 cookie 位于第一个 重定向前的页面。我得到的cookie是:set-cookie': 'datr=F*****F-OQsTCnbw0Caa;过期=2013 年 9 月 6 日星期五 20:33:26 GMT; 路径=/;域名=.facebook.com; httponly'

但是这些 cookie 不足以使会话处于活动状态。在 浏览器我得到了一个更大的 cookie:

datr=EzIL*********e-JFJL; lu=wgrr3q*********;语言环境=es_LA; L=2;

act=13*****; m_user=***%40gmail.com%3A526402587%3AN******%3A0% 3Av_1%2Cajax_1%2Cwidth_1600%2Cpxr_1%2Cgps_1%3A13***1099; p=182; fl=1; W=13****99; reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2Flogin.php%3Fm%3Dm%26refsrc%3Dhttp%253A%252F%252Fm.facebook.com%252F%26refid%3D0

可能是什么问题?

Fixed, httplin2 can't handle more than one cookie, the patch can be found here: http://code.google.com/p/httplib2/issues/detail?id=146

I am coding a facebook login script to get my last notifications but
I'm having a problem. I've changed from urllib to httplib2 because it
seemed easier and faster to me. Using the urllib I was able to get the
correct cookie and use it when ever I needed to retrieve the
notifications, now I'm using the example from the httplib page:

user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN;

rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8'
headers = {'User-Agent' : user_agent, 'Content-type':
'application/x-www-form-urlencoded', 'Accept' :
'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8',
'Connection' : 'keep-alive', 'Accept-Encoding' : 'gzip, deflate',
'Referer' : 'http://m.facebook.com/' }
data = {'email' : 'myusername', 'pass' : 'mypassword',
'post_form_id' : '', 'charset_test' : '', 'version': 1, 'ajax' : 1,
'pxr' : 1, 'gps' : 1, 'login' : 'Entrar'}
response, content =
h.request("http://www.facebook.com/login.php?m=m&refsrc=http://m.facebook.com/&refid=0",
"POST", urllib.parse.urlencode(data), headers=headers)

And this is the header I'm getting:

{'status': '200', 'x-cnection': 'close', 'content-location':

'http://m.facebook.com/home.php?refsrc=http%3A%2F%2Fm.facebook.com%2F&refid=0&m_sess=***tAm60&_rdr',
'transfer-encoding': 'chunked', 'set-cookie':
'datr=*****
*F-OQsTCnbw0Caa; expires=Fri, 06-Sep-2013 20:33:26 GMT;
path=/; domain=.facebook.com; httponly', 'expires': 'Sat, 01 Jan 2000
00:00:00 GMT', 'x-fb-server': '10.62.188.41', 'content-length':
'26702', '-content-encoding': 'gzip', 'pragma': 'no-cache',
'cache-control': 'private, no-cache, no-store, must-revalidate',
'date': 'Wed, 07 Sep 2011 20:33:27 GMT', 'p3p': 'CP="Facebook does not
have a P3P policy. Learn why here: http://fb.me/p3p"', 'content-type':
'text/html; charset=utf-8', 'x-frame-options': 'DENY'}

If I don't use the h.follow_all_redirects = True option, it doesn't
redirect me to the home page, but the set cookies are on the first
page before redirecting. The cookies I get are: set-cookie':
'datr=F*******F-OQsTCnbw0Caa; expires=Fri, 06-Sep-2013 20:33:26 GMT;
path=/; domain=.facebook.com; httponly'

But those cookies aren't enough to get the session active. In the
browser I'm getting a larger cookie:

datr=EzIL*********e-JFJL; lu=wgrr3q**********; locale=es_LA; L=2;

act=13*****;
m_user=***%40gmail.com%3A526402587%3AN******%3A0%3Av_1%2Cajax_1%2Cwidth_1600%2Cpxr_1%2Cgps_1%3A13***1099;
p=182; fl=1; W=13
***
*99;
reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2Flogin.php%3Fm%3Dm%26refsrc%3Dhttp%253A%252F%252Fm.facebook.com%252F%26refid%3D0

What could be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李白 2024-12-10 08:36:42

已修复,httplin2 无法处理多个 cookie,可以在此处找到补丁:http://code.google.com/p/httplib2/issues/detail?id=146

Fixed, httplin2 can't handle more than one cookie, the patch can be found here: http://code.google.com/p/httplib2/issues/detail?id=146

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文