diff --git a/scripts/simpleTest.py b/scripts/simpleTest.py index 760c120..4503199 100644 --- a/scripts/simpleTest.py +++ b/scripts/simpleTest.py @@ -1,12 +1,8 @@ import http.client import json +import subprocess conn = http.client.HTTPConnection('localhost', 8888) -conn.request("GET", "/v1/token?email=test") -response = conn.getresponse() -responseStr = response.read() -user = json.loads(responseStr) -token = user["token"] params = """{ "level": "medium", @@ -15,13 +11,25 @@ params = """{ }""" def getCaptcha(): - conn.request("POST", "/v1/captcha", body=params, headers={'access-token': user["token"]}) + conn.request("POST", "/v1/captcha", body=params) response = conn.getresponse() if response: responseStr = response.read() return json.loads(responseStr) +def getAndSolve(idStr): + conn.request("GET", "/v1/media?id=" + idStr) + response = conn.getresponse() + + if response: + responseBytes = response.read() + with open("captcha.png", "wb") as f: + f.write(responseBytes) + ocrResult = subprocess.Popen("gocr captcha.png", shell=True, stdout=subprocess.PIPE) + ocrAnswer = ocrResult.stdout.readlines()[0].strip().decode() + return ocrAnswer + def postAnswer(captchaId, ans): reply = {"answer": ans, "id" : captchaId} conn.request("POST", "/v1/answer", json.dumps(reply)) @@ -33,6 +41,6 @@ def postAnswer(captchaId, ans): for i in range(0, 10000): captcha = getCaptcha() - #print(captcha) captchaId = captcha["id"] - print(i, postAnswer(captchaId, "xyz")) + ans = getAndSolve(captchaId) + print(i, postAnswer(captchaId, ans))