lc-core/scripts/simpleTest.py

50 lines
1.3 KiB
Python
Raw Normal View History

2020-07-03 12:54:21 -04:00
import http.client
import json
import subprocess
import os
2020-07-03 12:54:21 -04:00
tempDir = os.getenv('XDG_RUNTIME_DIR', '.')
2020-07-03 12:54:21 -04:00
conn = http.client.HTTPConnection('localhost', 8888)
params = """{
"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():
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
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-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"]
ans = getAndSolve(captchaId)
print(i, postAnswer(captchaId, ans))