Python海报内容长度错误
我试图用模块发送图像。我遵循了这个例子,但它对我不起作用 我的代码:Python海报内容长度错误,python,image,post,enctype,poster,Python,Image,Post,Enctype,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"),
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`
`文件“decaptcha.py”,第27行,在
十杯茶(无)
文件“decaptcha.py”,第24行,在decaptcha中
solve=urllib2.urlopen(请求、数据源、标题)
urlopen中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第126行
return\u opener.open(url、数据、超时)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第390行,打开
req=甲基(req)
http_请求中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/poster-0.8.1-py2.7.egg/poster/streaminghttp.py”,第154行
“未为iterable正文指定内容长度”)
ValueError:未为iterable正文指定内容长度`
(免责声明:我没有使用海报库。建议的解决方案是我的最佳猜测。)
从海报文档来看,这似乎应该奏效
我将尝试以下方法(传递文件的内容而不是打开的文件迭代器,应该可以解决iterable正文问题):
建议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
您应该将datagen和标头传递给请求,而不是urlopen:
req = urllib2.Request("http://poster.decaptcher.com/", datagen, headers)
solve = urllib2.urlopen(req)
很抱歉,代码的格式设置不好,我将尝试修复它。非常感谢Aamir的格式设置谢谢您的回答,但即使使用指定的文件大小,它仍然不起作用。还是同样的错误。@Meph:你有没有弄明白?
req = urllib2.Request("http://poster.decaptcher.com/", datagen, headers)
solve = urllib2.urlopen(req)