What is an error in sending a post request with a header in python?

Source: Internet
Author: User
I want to send a request with the headers header. why is the error reported? {Code ...}

I want to send a requestheadersHeader. why is the error reported?

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)
  

Reply content:

I want to send a requestheadersHeader. why is the error reported?

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)
  

How to pass the urlopen parameters? please refer to the manual.

The answer for several places downstairs is inaccurate. if the parameter is incorrect, a key is required. urllib2.open (url, data = data, headers = header) is similar.

I posted so many tracks, but no error was reported.
Update: add @ Yunyu's answer. you can see that the headers parameter is not mentioned in his official document, however, the reason why headers can be passed and must be written as headers = is that writing is similar to writing a dict and then being processed as a requests object and passed to urlopen. If the headers parameter cannot be processed, the system reports the error "typeerror: urlopen got an unexpected keyword argument headers ", that's why I said you posted a lot of traceback but didn't post the last line of error.

An error occurred while passing the parameter.
Go to the flip manual and you will know that the third parameter of urllib2's urlopen is not headers, and there is no headers parameter at all.
The third parameter of the Request object of urllib2 is headers. Therefore, you need to construct a Request object first, and then the urllib2.urlopen parameter passes the Request object.

For urllib2, add a request header.

request = urllib2.Request(uri)request.add_header('User-Agent', 'fake-client')response = urllib2.urlopen(request)

I suggest you read the requests Library, which is similar to your method and is easy to use.

We recommend that you use 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.