您好,欢迎访问一九零五行业门户网

python发送带头部的post请求出错是怎么回事?

我想发送请求带上 headers 头部,请问为什么报错了?
traceback (most recent call last): file d:\python\get-email-by-tieba.py, line 49, in main() file d:\python\get-email-by-tieba.py, line 6, in main getthreadbytid() file d:\python\get-email-by-tieba.py, line 36, in getthreadbytid req = urllib2.urlopen(url, post_data, headers) file c:\python27\lib\urllib2.py, line 126, in urlopen return _opener.open(url, data, timeout) file c:\python27\lib\urllib2.py, line 391, in open response = self._open(req, data) file c:\python27\lib\urllib2.py, line 409, in _open '_open', req) file c:\python27\lib\urllib2.py, line 369, in _call_chain result = func(*args) file c:\python27\lib\urllib2.py, line 1173, in http_open return self.do_open(httplib.httpconnection, req) file c:\python27\lib\urllib2.py, line 1142, in do_open h.request(req.get_method(), req.get_selector(), req.data, headers) file c:\python27\lib\httplib.py, line 946, in request self._send_request(method, url, body, headers) file c:\python27\lib\httplib.py, line 987, in _send_request self.endheaders(body) file c:\python27\lib\httplib.py, line 940, in endheaders self._send_output(message_body) file c:\python27\lib\httplib.py, line 803, in _send_output self.send(msg) file c:\python27\lib\httplib.py, line 755, in send self.connect() file c:\python27\lib\httplib.py, line 736, in connect self.timeout, self.source_address) file c:\python27\lib\socket.py, line 557, in create_connection sock.settimeout(timeout) file c:\python27\lib\socket.py, line 222, in meth return getattr(self._sock,name)(*args)

回复内容:
我想发送请求带上 headers 头部,请问为什么报错了?
traceback (most recent call last): file d:\python\get-email-by-tieba.py, line 49, in main() file d:\python\get-email-by-tieba.py, line 6, in main getthreadbytid() file d:\python\get-email-by-tieba.py, line 36, in getthreadbytid req = urllib2.urlopen(url, post_data, headers) file c:\python27\lib\urllib2.py, line 126, in urlopen return _opener.open(url, data, timeout) file c:\python27\lib\urllib2.py, line 391, in open response = self._open(req, data) file c:\python27\lib\urllib2.py, line 409, in _open '_open', req) file c:\python27\lib\urllib2.py, line 369, in _call_chain result = func(*args) file c:\python27\lib\urllib2.py, line 1173, in http_open return self.do_open(httplib.httpconnection, req) file c:\python27\lib\urllib2.py, line 1142, in do_open h.request(req.get_method(), req.get_selector(), req.data, headers) file c:\python27\lib\httplib.py, line 946, in request self._send_request(method, url, body, headers) file c:\python27\lib\httplib.py, line 987, in _send_request self.endheaders(body) file c:\python27\lib\httplib.py, line 940, in endheaders self._send_output(message_body) file c:\python27\lib\httplib.py, line 803, in _send_output self.send(msg) file c:\python27\lib\httplib.py, line 755, in send self.connect() file c:\python27\lib\httplib.py, line 736, in connect self.timeout, self.source_address) file c:\python27\lib\socket.py, line 557, in create_connection sock.settimeout(timeout) file c:\python27\lib\socket.py, line 222, in meth return getattr(self._sock,name)(*args)

urlopen的参数到底怎么传递可以看看手册
楼下几位的回答是不准确的,错在参数要加上键,urllib2.open(url,data=data,headers=header)类似这样的
贴了那么多track,却没有贴报的错,心塞
更新,补充下@云语的答案,你可以看到他给出的官方文档里面也没有提到有headers这个参数,但之所以可以传headers并且需要写成headers=的形式是因为这样写类似于写一个dict然后被处理成requests对象传给urlopen。而如果确实不能处理headers这个参数,那也会报错“typeerror:urlopen got an unexpected keyword argument headers”,所以我才说你贴了很多traceback却没有把最后一行报的错贴出来。
参数传错了
去翻翻手册, 就知道urllib2的urlopen第三个参数并不是headers, 并且同时根本也没有headers参数.
然后构造urllib2的request对象的第三个参数才是headers, 所以你需要先构建一个request对象, 然后urllib2.urlopen的参数传递这个request对象
对于urllib2, 要加请求头,要这样写
request = urllib2.request(uri)request.add_header('user-agent', 'fake-client')response = urllib2.urlopen(request)
题主那种写法不对,建议你看下requests库,写法就像你那种写法,比较好用.
建议用requests吧
import requestsurl = ''data = {}headers = {}g = requests.get(url, data=data, headers=headers)p = requests.post(url, data=data, headers=headers)print g.text, p.text
其它类似信息

推荐信息