def submit(self, captcha, captchaType="file", match=None):
try:
img = Image.open(captcha)
output = StringIO.StringIO()
self.log_debug("CAPTCHA IMAGE", img, img.format, img.mode)
if img.format in ("GIF", "JPEG"):
img.save(output, img.format)
else:
if img.mode != "RGB":
img = img.convert("RGB")
img.save(output, "JPEG")
data = output.getvalue()
output.close()
except Exception, e:
raise CaptchaBrotherhoodException(
"Reading or converting captcha image failed: %s" % e)
req = get_request()
url = "%ssendNewCaptcha.aspx?%s" % (self.API_URL,
urllib.urlencode({'username': self.config.get('username'),
'password': self.config.get('password'),
'captchaSource': "pyLoad",
'timeout': "80"}))
req.c.setopt(pycurl.URL, url)
req.c.setopt(pycurl.POST, 1)
req.c.setopt(pycurl.POSTFIELDS, data)
req.c.setopt(pycurl.HTTPHEADER, ["Content-Type: text/html"])
try:
req.c.perform()
res = req.getResponse()
except Exception, e:
raise CaptchaBrotherhoodException("Submit captcha image failed")
req.close()
if not res.startswith("OK"):
raise CaptchaBrotherhoodException(res[1])
ticket = res[3:]
for _i in range(15):
time.sleep(5)
res = self.api_response("askCaptchaResult", ticket)
if res.startswith("OK-answered"):
return ticket, res[12:]
raise CaptchaBrotherhoodException("No solution received in time")
评论列表
文章目录