我如何用python读取cookie
我尝试用谷歌查找,我没有找到任何例子,我尝试使用堆栈溢出,我发现主题很有帮助,但最终它没有给我想要的结果。 我想做的是从 php 脚本“http://127.0.0.1/ 创建一个 cookie web/accounts/login.php" 它用 [user]=>dwaik 保存一个 cookie,我尝试从另一个 php 脚本读取该 cookie"http://127.0.0.1/web/accounts/read_cookie.php" 并成功读取!问题是我无法使用 python 读取它,使用
from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib
cj = cookielib.CookieJar()
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler())
req = Request("http://127.0.0.1/web/accounts/login.php")
f = opener.open(req)
print "the cookies are: "
for cookie in cj:
print cookie
此代码段取自 Retriving all Cookies in Python 它没有读取我的cookie,但是我从谷歌浏览器和IE启动了login.php,将不胜感激
i tried looking that up with google, i found no examples, i tried with stack overflow i found topics were helpful but at the end it didnt gave me the desired result.
what am trying to do is create a cookie from a php script "http://127.0.0.1/web/accounts/login.php" it saves a cookie with [user]=>dwaik, i tried reading that cookie from another php script "http://127.0.0.1/web/accounts/read_cookie.php" and it successfully reads it! the problem is i couldnt read it with python using the code
from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib
cj = cookielib.CookieJar()
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler())
req = Request("http://127.0.0.1/web/accounts/login.php")
f = opener.open(req)
print "the cookies are: "
for cookie in cj:
print cookie
this snippet was taken from Retrieving all Cookies in Python
it doesnt read my cookie, however i launched the login.php form google chrome and from IE, help would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议你使用机械化。
有关更多信息,我建议您访问 http://wwwsearch。 sourceforge.net/mechanize/doc.html#dealing-with-bad-html
还有一份为 mechanize 准备好的清单供您访问。
http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat- 。
我希望这个能对您有所帮助
I suggest you to use mechanize.
For more informaiton i suggest you to visit http://wwwsearch.sourceforge.net/mechanize/doc.html#dealing-with-bad-html
And a good prepared cheetsheet for mechanize for you can visit.
http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat-sheet
I hope this one helps out.