python發送帶頭部的post請求出錯是怎麼回事?

WBOY
發布: 2016-08-04 09:19:50
原創
2123 人瀏覽過

python發送帶頭部的post請求出錯是怎麼回事?

我想發送請求帶上 headers 頭部,請問為什麼報錯了?

<code>Traceback (most recent call last):
  File "D:\python\get-email-by-tieba.py", line 49, in <module>
    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)</code>
登入後複製
登入後複製

回覆內容:

python發送帶頭部的post請求出錯是怎麼回事?

我想發送請求帶上 headers 頭部,請問為什麼報錯了?

<code>Traceback (most recent call last):
  File "D:\python\get-email-by-tieba.py", line 49, in <module>
    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)</code>
登入後複製
登入後複製

urlopen的參數到底怎麼傳遞可以看看手冊
python發送帶頭部的post請求出錯是怎麼回事?

樓下幾位的答案是不準確的,錯在參數要加上鍵,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, 要加請求頭,要這樣寫

<code>request = urllib2.Request(uri)
request.add_header('User-Agent', 'fake-client')
response = urllib2.urlopen(request)</code>
登入後複製

題主那種寫法不對,建議你看下requests庫,寫法就像你那種寫法,比較好用.

建議用requests吧

<code>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</code>
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板