Skip to content

AutoVerify interface in Python

依赖环境

toml
[tool.poetry.dependencies]
python = "^3.9"
pyppeteer = "^1.0.2"
sanic = "^23.12.1"

代码

python
from sanic import Sanic
from sanic.response import file, json as resp_json
from cv import CaptchaVerifier

app = Sanic(__name__)
SECRET = 'aaaaaaaaa'
STATIC_DIR = './static/'

app.static('/static', './static')

@app.route('/')
async def serve_html(request):
    response = await file(STATIC_DIR + '/index.html')
    return response

cv = CaptchaVerifier()

@app.post("/verify")
async def verify_captcha(request):
    link_of_image = request.json.get('image')
    result = await cv.verify(link_of_image)
    return resp_json({"result": result}, status=200)

@app.add_task
async def run_captcha_verifier():
    print('[+] Started captcha verifier server')
    await cv.run()

if __name__ == '__main__':
    app.run(host="127.0.0.1", port=8488, access_log=True, auto_reload=True)
python
import traceback
from pyppeteer import launch

class CaptchaVerifier:

    def __init__(self):
        self.browser = None
        self.page = None

    @staticmethod
    def get_code(link_of_image):
        return f'''
        () => {{
            return new Promise((resolve, reject) => {{
                get_code_result('{link_of_image}').then((res) => {{
                    resolve(res);
                }})
                .catch((err) => {{
                    reject(err);
                }});
            }});
        }}
        '''

    async def run(self):
        try:
            edge_path = r'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'  # Replace with your Edge executable path
            self.browser = await launch(
                executablePath=edge_path,
                headless=True,
                args=['--js-flags="--experimental-wasm-simd"']
            )

            self.page = await self.browser.newPage()
            await self.page.goto('http://127.0.0.1:8488/')

        except Exception as e:
            traceback.print_exc()

    async def verify(self, link_of_image: str) -> str:
        try:
            code = self.get_code(link_of_image)
            result = await self.page.evaluate(code)
            return result
        except Exception as e:
            traceback.print_exc()
            return None

    async def close(self):
        await self.browser.close()
html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<title></title>
  <script src="./static/charset.js" ></script>
  <script src="./static/o.min.js" ></script>
  <script src="./static/model.js" ></script>
</head>
<body>
</body>
</html>

Readme

md
# usage
pip install poetry
poetry install
poetry run python app.py

# api
## request
curl --request POST \
  --url http://127.0.0.1:8488/verify \
  --header 'Content-Type: application/json' \
  --data '{
	"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIIAAAAoCAMAAAAFWtJHAAAAvVBMVEXz+/6UMBmXnJnGyKvTs7nTybvgsden4MzExMLcx7bT27fAq9Hn4eG3fG67gX3PrqijUEHLoqXbyMSfSTWrYlLDlYu6cHjWoL+wYGCrcFS+sayyjIKgVUOsem2WjomUPSmUSzmVZlmmZ1jBjnvDopKraVXLtaajVkGkyrWlVUObQy2zfGqcQi7cysiVc2mfTkekXl6vfIy6m7q1jKOVWEmqbXWWgXmWRi+bQC2itJ+diHKbclyYXEWWNiCYOSN2Xpt/AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAEgklEQVRYhbWYCXfUNhCA5bW99sYSaiQZ2pQEKOSAFlp6BZrC//9ZaA4dtmVnw4N5+7xZH5pvbjmiFW0r+l4UpEYpXfEyeFm5FJ5Ox+3bWmDo+/aha3wThN4bv41QbyzhAbYZjkIABkKAT5FhKxCixHByko5bIo9DQBvWAyFKDGsIkzu1quxRCHW96YayeOX0WUOQ2lUg5hiE7UA8HKHv+7N3pL9yo0wIouvgBx1nBOn4AIR9RJDSaD0q5xQg9MPwe8ViKRXuRfgqOWER2lZRLNawEH9Uz2YMJF45fb6JgA/AFcJkBA694N3y/s9wzn1HhH04asN+0HAGEcTwl//9d87QNN8RQegUhojwD9TCCzytcgSSzZWlXL9mFgx7RIDkd8ENA7Wy5/6n2JEfxoiADrjPCap61Pjb6YEZQeUmfKAfECAZnAxuwH4+7PDX7t8UodKKRQGf/nDalJ6QNoR75gqH51W0F+QCsPTzkJMGFjyCAVKcMmuuCIUuqQWCIfPRDVVw07tYJIHhKC9Qm0Frosd3u3SUblnogjPBN6Ys9USnYmvU3B6ODoTg/LZ6iSCETbUXpH1VTYRStruFqNx00abKnjbTbC474SQheHvkEkFVuamEcMkOt86mLnCDQeHUp2nx4UN1v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