我如何用python读取cookie

发布于 2024-11-07 16:22:02 字数 949 浏览 0 评论 0原文

我尝试用谷歌查找,我没有找到任何例子,我尝试使用堆栈溢出,我发现主题很有帮助,但最终它没有给我想要的结果。 我想做的是从 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 技术交流群。

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

发布评论

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

评论(1

仅此而已 2024-11-14 16:22:02

我建议你使用机械化。

import cookielib
import urllib2
import mechanize

br = mechanize.Browser()
cookiejar = cookielib.LWPCookieJar()
br.set_cookiejar( cookiejar )
br.set_proxies({"http": "yourProxyHereIfneeded","https": 
"yourProxyHereIfneeded"})
br.set_handle_equiv( True )
br.set_handle_gzip( True )
br.set_handle_redirect( True ) 
br.set_handle_referer( True )
br.set_handle_robots( False )

br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), 
max_time = 1)

br.addheaders = [ ( 'User-agent', 'yourHeadervalueifNeeded' ) ]

#this one will open you what your desired domain
response = br.open("theDomain")

#and this one stands for saving the cookies for you.
cookiejar.save('cookies.txt', ignore_discard=True, 
ignore_expires=True)

#after you saved your cookie a txt or dump with pickle for example. 
#You can easly load it while configure the browser Object at the start 
#our example code.

br = mechanize.Browser()
cookiejar = cookielib.LWPCookieJar()
cookiejar.load('cookie_login.txt', ignore_discard=True, 
ignore_expires=True)
br.set_cookiejar( cookiejar )

有关更多信息,我建议您访问 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.

import cookielib
import urllib2
import mechanize

br = mechanize.Browser()
cookiejar = cookielib.LWPCookieJar()
br.set_cookiejar( cookiejar )
br.set_proxies({"http": "yourProxyHereIfneeded","https": 
"yourProxyHereIfneeded"})
br.set_handle_equiv( True )
br.set_handle_gzip( True )
br.set_handle_redirect( True ) 
br.set_handle_referer( True )
br.set_handle_robots( False )

br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), 
max_time = 1)

br.addheaders = [ ( 'User-agent', 'yourHeadervalueifNeeded' ) ]

#this one will open you what your desired domain
response = br.open("theDomain")

#and this one stands for saving the cookies for you.
cookiejar.save('cookies.txt', ignore_discard=True, 
ignore_expires=True)

#after you saved your cookie a txt or dump with pickle for example. 
#You can easly load it while configure the browser Object at the start 
#our example code.

br = mechanize.Browser()
cookiejar = cookielib.LWPCookieJar()
cookiejar.load('cookie_login.txt', ignore_discard=True, 
ignore_expires=True)
br.set_cookiejar( cookiejar )

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.

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