What's going on when python sends a post request with a header?

WBOY
Release: 2016-08-04 09:19:50
Original
2123 people have browsed it

What's going on when python sends a post request with a header?

I want to send a request with headers headers. Why is an error reported?

<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>
Copy after login
Copy after login

Reply content:

What's going on when python sends a post request with a header?

I want to send a request with headers headers. Why is an error reported?

<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>
Copy after login
Copy after login

How to pass the parameters of urlopen can be read in the manual
What's going on when python sends a post request with a header?

The answers given by the people downstairs are inaccurate. The mistake is that the key must be added to the parameters. urllib2.open(url,data=data,headers=header) is similar to this

I have posted so many tracks, but there is no error in posting, I am heartbroken
Update, I will supplement @Yunyu’s answer. You can see that the official document he gave does not mention the parameter headers, but the reason Headers can be passed and need to be written in the form of headers= because writing this way is similar to writing a dict and then processing it into a requests object and passing it to urlopen. And if it is true that the headers parameter cannot be processed, the error "typeerror: urlopen got an unexpected keyword argument headers" will also be reported, so I said that you posted a lot of tracebacks but did not post the error reported in the last line.

The parameter is passed wrong
If you look through the manual, you will know that the third parameter of urllib2's urlopen is not headers, and there is no headers parameter at all.
Then the third parameter of constructing the Request object of urllib2 is headers, so you You need to build a Request object first, and then pass this Request object as the parameter of urllib2.urlopen

For urllib2, you need to add a request header and write it like this

<code>request = urllib2.Request(uri)
request.add_header('User-Agent', 'fake-client')
response = urllib2.urlopen(request)</code>
Copy after login

The way the question is written is wrong. I suggest you take a look at the requests library. The way it is written is like yours, which is easier to use.

It is recommended to use 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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template