当前位置:Gxlcms > PHP教程 > python发送带头部的post请求出错是怎么回事?

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

时间:2021-07-01 10:21:17 帮助过:30人阅读

我想发送请求带上 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 requests
url = ''
data = {}
headers = {}

g = requests.get(url, data=data, headers=headers)
p = requests.post(url, data=data, headers=headers)

print g.text, p.text

人气教程排行