我试图用poster模块发送图片。我按照这个例子,但它对我不起作用
我的代码:
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib, urllib2
def decaptcha(hash):
register_openers()
params = {
"file": open("captcha.jpg", "rb"),
"function" : "picture2",
"username" : "uname",
"password" : "pwd",
"pict_to" : 0,
"pict_type" : 0
}
datagen, headers = multipart_encode(params)
req = urllib2.Request("http://poster.decaptcher.com/")
solve = urllib2.urlopen(req, datagen, headers)
print solve.read()
decaptcha(None)
并追溯:
`File "decaptcha.py", line 27, in <module>
decaptcha(None)
File "decaptcha.py", line 24, in decaptcha
solve = urllib2.urlopen(req, datagen, headers)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 390, in open
req = meth(req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/poster-0.8.1-py2.7.egg/poster/streaminghttp.py", line 154, in http_request
"No Content-Length specified for iterable body")
ValueError: No Content-Length specified for iterable body`
答案 0 :(得分:0)
(免责声明:我没有使用海报库。建议的解决方案是我最好的猜测。)
从海报文档来看,它看起来应该有效。
我会尝试以下方法(传递文件的内容而不是打开的文件迭代器,应修复可迭代的主体问题):
params = {
"file": open("captcha.jpg", "rb").read(),
"function" : "picture2",
"username" : "uname",
"password" : "pwd",
"pict_to" : 0,
"pict_type" : 0
}
建议2:
或尝试: 来自multipart.encode导入MultiPartParam
params = [
MultiPartParam("file", fileobj=open("captcha.jpg", "rb")),
("function", picture2"),
("username", "uname"),
("password", "pwd"),
("pict_to", 0),
("pict_type", 0),
]
如果失败并出现同样的错误,请尝试将filesize
参数指定为MultiPartParam
。
答案 1 :(得分:0)
您应该将datagen和标题传递给Request,而不是urlopen:
req = urllib2.Request("http://poster.decaptcher.com/", datagen, headers)
solve = urllib2.urlopen(req)