利用poster模块发一个post文件请求,如何获取发出请求的头信息?(高分)

haoshuaiya111 2011-11-04 08:33:43
求教一个问题,我用以下代码发出的请求,我如何才能获取到我发出请求的头部信息呢?谢谢!
我用的第三方模块poster来发的请求。

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()


我在网上找的方法,但是会报错,

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模块的问题吗?如何更好的解决呢?
...全文
2177 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
panghuhu250 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 haoshuaiya111 的回复:]
大神request.data获取不到数据啊!如何使用?
[/Quote]

"获取不到数据"太模糊了。给出例子:你怎么构造的request,request.data的结果是什么,为什么它不是你想要的。

针对15楼的回答,你应该解释为什么datagen不行,而不是问另一个问题。
haoshuaiya111 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 iambic 的回复:]
你的data不就是你构造Request的时候传进去的datagen么,还获取什么啊。
[/Quote]
大神request.data获取不到数据啊!如何使用?
iambic 2011-11-11
  • 打赏
  • 举报
回复
你的data不就是你构造Request的时候传进去的datagen么,还获取什么啊。
haoshuaiya111 2011-11-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 panghuhu250 的回复:]
引用 10 楼 haoshuaiya111 的回复:
如果是一个post文件请求(就是这个例子),怎么获取body信息呢?

不确定body指什么,是指request带的data吗?

Python code

request.data
8: <poster.encode.multipart_yielder instance at 0xb70494ac>
[/Quote]
对,是post请求的data信息,用request.data方法怎么获取data啊?之前试验了一下,没实现成功!
haoshuaiya111 2011-11-10
  • 打赏
  • 举报
回复
顶一下,期待答案中!
haoshuaiya111 2011-11-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 panghuhu250 的回复:]
引用 4 楼 haoshuaiya111 的回复:
...我还怎么获取发出请求的头信息啊?


是指这个吗?

Python code

>>> request.header_items()
6: [('Host', 'api.qcloud.cn'),
('Content-type',
'multipart/form-data; boundary=579d9a2e528……
[/Quote]

给力,赞一个!可以解决问题,request.headers()方法也可以获取一个字典类型的头信息,但是还有个问题这个方法只能获取头信息,如果是一个post文件请求(就是这个例子),怎么获取body信息呢?我没有在request对象中找到这样的方法,如果有方法能把body信息也搞出来吗?那就完美了。
panghuhu250 2011-11-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 haoshuaiya111 的回复:]
如果是一个post文件请求(就是这个例子),怎么获取body信息呢?
[/Quote]

不确定body指什么,是指request带的data吗?

request.data
8: <poster.encode.multipart_yielder instance at 0xb70494ac>
livesguan 2011-11-08
  • 打赏
  • 举报
回复
查一下这个第三方模块poster有没有相应的方法得到请求头部信息.应该有的.
haoshuaiya111 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 panghuhu250 的回复:]
[Quote=引用楼主 haoshuaiya111 的回复:]

Python code

import urllib2
import poster

opener = poster.streaminghttp.register_openers()
url='http://api.qcloud.cn/intf.php?method=Qds.put&v=1&key=d……
[/Quote]
应该是这个原因,但是去掉这两句代码,我还怎么获取发出请求的头信息啊?
阿小信 2011-11-08
  • 打赏
  • 举报
回复
+1
[Quote=引用 7 楼 panghuhu250 的回复:]

引用 4 楼 haoshuaiya111 的回复:
...我还怎么获取发出请求的头信息啊?


是指这个吗?
Python code
>>> request.header_items()
6: [('Host', 'api.qcloud.cn'),
('Content-type',
'multipart/form-data; boundary=579d9a2e52824d4bb……
[/Quote]
panghuhu250 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 haoshuaiya111 的回复:]
...我还怎么获取发出请求的头信息啊?
[/Quote]

是指这个吗?
>>> request.header_items()
6: [('Host', 'api.qcloud.cn'),
('Content-type',
'multipart/form-data; boundary=579d9a2e52824d4bbfd81e9d8f35cd76'),
('Content-length', '1491466'),
('User-agent', 'Python-urllib/2.6')]
haoshuaiya111 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 userguanguan 的回复:]
查一下这个第三方模块poster有没有相应的方法得到请求头部信息.应该有的.
[/Quote]
好像没有诶,你确定有吗?
haoshuaiya111 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 panghuhu250 的回复:]
引用楼主 haoshuaiya111 的回复:
错误信息:
send: 'POST http://api.qcloud.cn/intf.php?method=Qds.put&v=1&key=desktop-weiboauth-87000945&lsig=e3f9e50869aa31251870e542dda4c90a&cs=2b2029835e1a53f2e0f……
[/Quote]

result = urllib2.urlopen(request)
panghuhu250 2011-11-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 haoshuaiya111 的回复:]
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()

[/Quote]

问题可能是urllib2的default opener里没有poster的特殊的httphandler:你的代码第一句加上了这个特殊的handler,但下面两行代码又把urllib2的default opener换成了一个新的opener。去掉下面两行:

opener = urllib2.build_opener(httpHandler)
urllib2.install_opener(opener)
panghuhu250 2011-11-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 haoshuaiya111 的回复:]
错误信息:
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>
[/Quote]

line 38在你贴出的代码里是哪一行?

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧