2020-07-03 12:54:21 -04:00
|
|
|
import http.client
|
|
|
|
import json
|
2021-04-14 00:17:22 -04:00
|
|
|
import subprocess
|
2021-04-14 00:30:36 -04:00
|
|
|
import os
|
2020-07-03 12:54:21 -04:00
|
|
|
|
2021-04-14 00:30:36 -04:00
|
|
|
tempDir = os.getenv('XDG_RUNTIME_DIR', '.')
|
2020-07-03 12:54:21 -04:00
|
|
|
conn = http.client.HTTPConnection('localhost', 8888)
|
|
|
|
|
|
|
|
params = """{
|
2021-04-14 00:30:58 -04:00
|
|
|
"level": "debug",
|
2020-07-03 12:54:21 -04:00
|
|
|
"media": "image/png",
|
|
|
|
"input_type": "text"
|
|
|
|
}"""
|
|
|
|
|
2020-07-04 15:18:20 -04:00
|
|
|
def getCaptcha():
|
2021-04-14 00:17:22 -04:00
|
|
|
conn.request("POST", "/v1/captcha", body=params)
|
2020-07-04 15:18:20 -04:00
|
|
|
response = conn.getresponse()
|
2020-07-03 12:54:21 -04:00
|
|
|
|
2020-07-04 15:18:20 -04:00
|
|
|
if response:
|
|
|
|
responseStr = response.read()
|
|
|
|
return json.loads(responseStr)
|
2020-07-03 12:54:21 -04:00
|
|
|
|
2021-04-14 00:17:22 -04:00
|
|
|
def getAndSolve(idStr):
|
|
|
|
conn.request("GET", "/v1/media?id=" + idStr)
|
|
|
|
response = conn.getresponse()
|
|
|
|
|
|
|
|
if response:
|
|
|
|
responseBytes = response.read()
|
2021-04-14 00:30:36 -04:00
|
|
|
fileName = tempDir + "/captcha.png"
|
|
|
|
with open(fileName, "wb") as f:
|
2021-04-14 00:17:22 -04:00
|
|
|
f.write(responseBytes)
|
2021-04-14 00:30:36 -04:00
|
|
|
ocrResult = subprocess.Popen("gocr " + fileName, shell=True, stdout=subprocess.PIPE)
|
2021-04-14 00:17:22 -04:00
|
|
|
ocrAnswer = ocrResult.stdout.readlines()[0].strip().decode()
|
|
|
|
return ocrAnswer
|
|
|
|
|
2020-07-04 15:18:20 -04:00
|
|
|
def postAnswer(captchaId, ans):
|
|
|
|
reply = {"answer": ans, "id" : captchaId}
|
2020-07-03 12:54:21 -04:00
|
|
|
conn.request("POST", "/v1/answer", json.dumps(reply))
|
|
|
|
response = conn.getresponse()
|
|
|
|
if response:
|
2020-07-04 15:18:20 -04:00
|
|
|
return response.read()
|
2020-07-03 12:54:21 -04:00
|
|
|
print(responseStr)
|
|
|
|
|
|
|
|
|
2020-07-04 15:18:20 -04:00
|
|
|
for i in range(0, 10000):
|
|
|
|
captcha = getCaptcha()
|
|
|
|
captchaId = captcha["id"]
|
2021-04-14 00:17:22 -04:00
|
|
|
ans = getAndSolve(captchaId)
|
|
|
|
print(i, postAnswer(captchaId, ans))
|