简体   繁体   English

如何在python中使用urllib2发送cookie和phpssid?

[英]How to send cookie and phpssid with urllib2 in python?

I wonder how can I send cookie and phpssid with urllib2 in python? 我想知道如何在python中使用urllib2发送cookie和phpssid? Actually I want to read a page I've logged in with my browser, but when I try to read it with this script I encounter a text which seems to say that you've missed something. 实际上,我想阅读使用浏览器登录的页面,但是当我尝试使用此脚本阅读该页面时,遇到了一段文字,似乎表明您错过了某些内容。 My script : 我的剧本:

#!/usr/bin/python
import urllib2
f = urllib2.urlopen('http://mywebsite.com/sub/create.php?id=20')
content = f.read()
file = open('file.txt', 'w')
file.write(content)
file.close()

The error message I save instead of the real page : 我保存的错误消息而不是实际页面:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/number/domains/1number.com/public_html/s4/app/mywidgets.php:1) in /home/number/domains/1number.com/public_html/s4/app/mywidgets.php on line 23

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/number/domains/1number.com/public_html/s4/app/mywidgets.php:1) in /home/number/domains/1number.com/public_html/s4/app/mywidgets.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /home/number/domains/1number.com/public_html/s4/app/mywidgets.php:1) in /home/number/domains/1number.com/public_html/s4/lib/webservice.php on line 0

What is the exact problem?(Please give me a simple way to implement what I want) 确切的问题是什么?(请给我一种简单的方法来实现我想要的东西)

Thanks in advance 提前致谢

For the SID, one of the ways to send that is as part of the query string, and you're already doing that. 对于SID,其中一种发送方式是作为查询字符串的一部分,而您已经在这样做了。 At least I assume that's what the id=20 part of your URL is. 至少我认为这就是URL的id=20部分。

For cookies, everything you want is in cookielib . 对于cookie,所需的一切都在cookielib

Just creating a CookieJar to use for a session with the server is trivial. 仅仅创建一个CookieJar来与服务器进行会话是微不足道的。 If you want to import cookies from your browser, there are three possibilities: 如果要从浏览器导入cookie,则有以下三种可能性:

  1. If your browser uses the old Netscape cookie file format, you can use FileCookieJar . 如果您的浏览器使用旧的Netscape cookie文件格式,则可以使用FileCookieJar
  2. If your browser uses a sqlite database (as at least Firefox and Safari/Chrome do), use the sqlite3 module to read it, and populate a CookieJar manually. 如果您的浏览器使用sqlite数据库(至少像Firefox和Safari / Chrome一样),请使用sqlite3模块读取它,并手动填充CookieJar
  3. If worst comes to worst, copy and paste the cookies from your browser into your script as hardcoded strings and popular a CookieJar manually. 如果情况变得更糟,则将浏览器中的cookie作为硬编码字符串复制并粘贴到脚本中,并手动流行CookieJar

If you don't want to read the docs on how to use cookielib , just see the examples at the end, which show how to use a CookieJar with urllib2 , which is exactly what you want to do. 如果您不想阅读有关如何使用cookielib的文档,请参阅最后的示例 ,这些示例演示如何将CookieJarurllib2一起使用,这正是您想要做的。

If you have a problem, read the docs. 如果您有问题,请阅读文档。

Meanwhile, what you're showing us are (a) warnings, not errors, and (b) obviously a problem on the server side, not your script. 同时,您向我们展示的是(a)警告而非错误,以及(b)显然是服务器端的问题,而不是您的脚本。 The server should never be spewing out a bunch of warnings and an otherwise-blank page. 服务器永远不会发出一堆警告和空白页面。 If you, or one of your coworkers, is responsible for the server code, that needs to be fixed first (and your current simple Python script can serve as a great regression test case). 如果您或您的一个同事负责服务器代码,则需要首先修复该问题(并且您当前的简单Python脚本可以用作出色的回归测试用例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM