I was trying to send image with poster module. I followed the example, but it doesn't work for me
My code:
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)
And traceback:
`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开发者_如何学JAVA-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`
(Disclaimer: I have not used the poster library. The suggested solution is my best guess.)
From the poster docs, it looks as if this should work.
I would try the following (passes the file's contents instead of the open file iterator, should fix the iterable body issue):
params = {
"file": open("captcha.jpg", "rb").read(),
"function" : "picture2",
"username" : "uname",
"password" : "pwd",
"pict_to" : 0,
"pict_type" : 0
}
Suggestion 2:
Or try: from multipart.encode import MultiPartParam
params = [
MultiPartParam("file", fileobj=open("captcha.jpg", "rb")),
("function", picture2"),
("username", "uname"),
("password", "pwd"),
("pict_to", 0),
("pict_type", 0),
]
If this fails with the same error, try specifying the filesize
parameter to MultiPartParam
.
You should pass datagen and headers to Request, not urlopen:
req = urllib2.Request("http://poster.decaptcher.com/", datagen, headers)
solve = urllib2.urlopen(req)
精彩评论