利用poster模块发一个post文件请求,怎么获取发出请求的头信息?(高分)
利用poster模块发一个post文件请求,如何获取发出请求的头信息?(高分)
求教一个问题,我用以下代码发出的请求,我如何才能获取到我发出请求的头部信息呢?谢谢!
我用的第三方模块poster来发的请求。
我在网上找的方法,但是会报错,
错误信息:
send: 'POST http://api.qcloud.cn/intf.php?method=Qds.put&v=1&key=desktop-weiboauth-87000945&lsig=e3f9e50869aa31251870e542dda4c90a&cs=2b2029835e1a53f2e0f233e14973cfb4 HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 181\r\nHost: api.qcloud.cn\r\nContent-Type: multipart/form-data; boundary=b673099801d548aa9e4cef450e9530df\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n'
Traceback (most recent call last):
File "E:\workspace\fgos\TestGood\post_test.py", line 38, in <module>
send: <poster.encode.multipart_yielder instance at 0x011FE288>
result = urllib2.urlopen(request)
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 394, in open
response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 412, in _open
'_open', req)
File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1199, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python27\lib\urllib2.py", line 1168, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "C:\Python27\lib\httplib.py", line 955, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 989, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 951, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 815, in _send_output
self.send(message_body)
File "C:\Python27\lib\httplib.py", line 787, in send
self.sock.sendall(data)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: must be string or buffer, not instance
如果我去掉“request = urllib2.Request(url,datagen, headers)”这句的datagen和header参数,就可以获取到头信息。
但是我需要获取的确是发出一个post文件请求的头信息,这怎么搞定啊,是poster模块的问题吗?如何更好的解决呢?
------解决方案--------------------
求教一个问题,我用以下代码发出的请求,我如何才能获取到我发出请求的头部信息呢?谢谢!
我用的第三方模块poster来发的请求。
- Python code
import urllib2 import poster opener = poster.streaminghttp.register_openers() url='http://api.qcloud.cn/intf.php?method=Qds.put&v=1&key=desktop-weiboauth-87000945&lsig=e3f9e50869aa31251870e542dda4c90a&cs=2b2029835e1a53f2e0f233e14973cfb4' params = {'file': open(r"e:\1.txt","rb")} datagen, headers = poster.encode.multipart_encode(params) request = urllib2.Request(url,datagen, headers) result = urllib2.urlopen(request) print result.read(),result.info()
我在网上找的方法,但是会报错,
- Python code
import urllib2 import poster opener = poster.streaminghttp.register_openers() url='http://api.qcloud.cn/intf.php?method=Qds.put&v=1&key=desktop-weiboauth-87000945&lsig=e3f9e50869aa31251870e542dda4c90a&cs=2b2029835e1a53f2e0f233e14973cfb4' httpHandler = urllib2.HTTPHandler(debuglevel=1) #httpsHandler = urllib2.HTTPSHandler(debuglevel=1) opener = urllib2.build_opener(httpHandler) urllib2.install_opener(opener) params = {'file': open(r"e:\1.txt","rb")} datagen, headers = poster.encode.multipart_encode(params) request = urllib2.Request(url,datagen, headers) result = urllib2.urlopen(request) print result.read(),result.info()
错误信息:
send: 'POST http://api.qcloud.cn/intf.php?method=Qds.put&v=1&key=desktop-weiboauth-87000945&lsig=e3f9e50869aa31251870e542dda4c90a&cs=2b2029835e1a53f2e0f233e14973cfb4 HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 181\r\nHost: api.qcloud.cn\r\nContent-Type: multipart/form-data; boundary=b673099801d548aa9e4cef450e9530df\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n'
Traceback (most recent call last):
File "E:\workspace\fgos\TestGood\post_test.py", line 38, in <module>
send: <poster.encode.multipart_yielder instance at 0x011FE288>
result = urllib2.urlopen(request)
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 394, in open
response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 412, in _open
'_open', req)
File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1199, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python27\lib\urllib2.py", line 1168, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "C:\Python27\lib\httplib.py", line 955, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 989, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 951, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 815, in _send_output
self.send(message_body)
File "C:\Python27\lib\httplib.py", line 787, in send
self.sock.sendall(data)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: must be string or buffer, not instance
如果我去掉“request = urllib2.Request(url,datagen, headers)”这句的datagen和header参数,就可以获取到头信息。
但是我需要获取的确是发出一个post文件请求的头信息,这怎么搞定啊,是poster模块的问题吗?如何更好的解决呢?
------解决方案--------------------