如何使用POST数据和Cookie制作Python HTTP请求?

时间:2012-02-25 15:48:10

标签: python cookies http-post urllib2

我正在尝试使用Python中的Cookie进行HTTP POST。

我有URL,POST数据和cookie的值。

import urllib2
url="http://localhost/testing/posting.php"
data="subject=Alice-subject&addbbcode18=%23444444&addbbcode20=0&helpbox=Close+all+open+bbCode+tags&message=alice-body&poll_title=&add_poll_option_text=&poll_length=&mode=newtopic&sid=5b2e663a3d724cc873053e7ca0f59bd0&f=1&post=Submit"
cookie = "phpbb2mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3A1%3A%223%22%3B%7D; phpbb2mysql_t=a%3A9%3A%7Bi%3A3%3Bi%3A1330156986%3Bi%3A1%3Bi%3A1330160737%3Bi%3A5%3Bi%3A1330161702%3Bi%3A6%3Bi%3A1330179284%3Bi%3A2%3Bi%3A1330160743%3Bi%3A7%3Bi%3A1330163187%3Bi%3A8%3Bi%3A1330164442%3Bi%3A9%3Bi%3A1330164739%3Bi%3A10%3Bi%3A1330176335%3B%7D; phpbb2mysql_sid=5b2e663a3d724cc873053e7ca0f59bd0"
#creating HTTP Req
req = urllib2.Request(url,data,cookie)

f = urllib2.urlopen(req)
print f.read()

但是,如果我尝试运行该程序,则会抛出错误:

Traceback (most recent call last):
  File "task-4.py", line 7, in <module>
    req = urllib2.Request(url,data,cookie)
  File "/usr/lib/python2.6/urllib2.py", line 197, in __init__
    for key, value in headers.items():
AttributeError: 'str' object has no attribute 'items'

我有两个问题:  1.我的HTTP POST请求是否正确? (我已经能够在Java中执行相同的操作并获得了一个成功发布到phpBB的HTTP 200,但是,我是Python的新手)  2.有人可以向我展示一个使用POST数据和cookie处理HTTP POST的玩具示例吗?

提前致谢,

罗伊

4 个答案:

答案 0 :(得分:34)

您可以尝试请求,这样可以在处理HTTP查询时更轻松。

import requests
url="http://localhost/testing/posting.php"
data= {
    'subject': 'Alice-subject',
    'addbbcode18': '%23444444',
    'addbbcode20': '0',
    'helpbox': 'Close all open bbCode tags',
    'message': 'alice-body',
    'poll_title': '',
    'add_poll_option_text': '',
    'poll_length': '',
    'mode': 'newtopic',
    'sid': '5b2e663a3d724cc873053e7ca0f59bd0',
    'f': '1',
    'post': 'Submit',
    }
 cookies = {'phpbb2mysql_data': 'a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3A1%3A%223%22%3B%7D',
    'phpbb2mysql_t': 'a%3A9%3A%7Bi%3A3%3Bi%3A1330156986%3Bi%3A1%3Bi%3A1330160737%3Bi%3A5%3Bi%3A1330161702%3Bi%3A6%3Bi%3A1330179284%3Bi%3A2%3Bi%3A1330160743%3Bi%3A7%3Bi%3A1330163187%3Bi%3A8%3Bi%3A1330164442%3Bi%3A9%3Bi%3A1330164739%3Bi%3A10%3Bi%3A1330176335%3B%7D', 
    'phpbb2mysql_sid': '5b2e663a3d724cc873053e7ca0f59bd0',
    }
print requests.get(url, data=data, cookies=cookies).text

http://python-requests.org/

答案 1 :(得分:3)

您传递的第三个参数是标题,应该是字典。这应该这样做

cookie = {"Cookie" : "phpbb2mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3A1%3A%223%22%3B%7D; phpbb2mysql_t=a%3A9%3A%7Bi%3A3%3Bi%3A1330156986%3Bi%3A1%3Bi%3A1330160737%3Bi%3A5%3Bi%3A1330161702%3Bi%3A6%3Bi%3A1330179284%3Bi%3A2%3Bi%3A1330160743%3Bi%3A7%3Bi%3A1330163187%3Bi%3A8%3Bi%3A1330164442%3Bi%3A9%3Bi%3A1330164739%3Bi%3A10%3Bi%3A1330176335%3B%7D; phpbb2mysql_sid=5b2e663a3d724cc873053e7ca0f59bd0"}

答案 2 :(得分:3)

我喜欢httplib:

from urlparse import urlparse
from httplib import HTTPConnection
url = "http://localhost/testing/posting.php"
data = "subject=Alice-subject&addbbcode18=%23444444&addbbcode20=0&helpbox=Close+all+open+bbCode+tags&message=alice-body&poll_title=&add_poll_option_text=&poll_length=&mode=newtopic&sid=5b2e663a3d724cc873053e7ca0f59bd0&f=1&post=Submit"
cookie = "phpbb2mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3A1%3A%223%22%3B%7D; phpbb2mysql_t=a%3A9%3A%7Bi%3A3%3Bi%3A1330156986%3Bi%3A1%3Bi%3A1330160737%3Bi%3A5%3Bi%3A1330161702%3Bi%3A6%3Bi%3A1330179284%3Bi%3A2%3Bi%3A1330160743%3Bi%3A7%3Bi%3A1330163187%3Bi%3A8%3Bi%3A1330164442%3Bi%3A9%3Bi%3A1330164739%3Bi%3A10%3Bi%3A1330176335%3B%7D; phpbb2mysql_sid=5b2e663a3d724cc873053e7ca0f59bd0"
urlparts = urlparse(url)
conn = HTTPConnection(urlparts.netloc, urlparts.port or 80)
conn.request("POST", urlparts.path, data, {'Cookie': cookie})
resp = conn.getresponse()
body = resp.read()

答案 3 :(得分:1)

  1. 不是。该错误是因为urllib2库试图迭代您给它的cookie字符串的项目。尝试使用:
  2. cookies = urllib.urlencode({'phpbb2mysql_data':'foo', 'autologinid':'blahblah'})
    # Can do the same for data, allowing you to store it as a map.
    headers = {'Cookie': cookies}
    req = urllib2.Request(url, data, headers)
    
    1. 请参阅python: urllib2 how to send cookie with urlopen request,但您最好的参考仍然是urllib2 Request docs,但是与一些较新的库相比,这是一个棘手(但功能强大)的库。