lc-core/scripts/simpleTest.py

50 lines
1.3 KiB
Python
Raw Normal View History

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