完美验证码改web服务

服务端打包exe

# coding=utf-8
'''
Created on 2022年6月8日

@author: 瞌睡蟲子
'''
import click
from ctypes import *
from os.path import join, dirname
from flask import Flask, request
from werkzeug.routing import BaseConverter
from wsgiref.simple_server import make_server


class RegexConverter(BaseConverter):
    def __init__(self, map, *args):
        self.map = map
        self.regex = args[0]


# web服务
app = Flask(__name__)
app.url_map.converters['regex'] = RegexConverter

dll = windll.LoadLibrary(join(dirname(__file__), 'WmCode.dll'))

# 这个函数用来向DLL说明传入的文本使用unicode格式
if (dll.UseUnicodeString(1, 1)):
    # UseUnicodeString调用一次即可,无需重复调用
    print('SetInUnicode Success:')
else:
    # 注意缩进格式
    print('etInUnicode Fail!')


@app.route('/load', methods=['POST'])
def load():
    f = request.files.get('dat')
    if f is None:
        # 表示没有发送文件
        return "未上传字库"
    data = f.read()
    pwd = request.form.get("password")
    ex = request.form.get("ex")
    if ex:
        return str(dll.LoadWmFromBufferEx(data, len(data), pwd))
    else:
        return str(dll.LoadWmFromBuffer(data, len(data), pwd))


@app.route('/option/', methods=['POST'])
def option(wmid):
    j = request.json
    if type(j) == list:
        if len(j) == 2 and type(j[0]) == int and type(j[1]) == int:
            if wmid:
                wmid = int(wmid)
                dll.SetWmOptionEx(wmid, j[0], j[1])
            else:
                dll.SetWmOption(j[0], j[1])
        elif type(j[0]) == list:
            for dt in j:
                if len(dt) == 2 and type(dt[0]) == int and type(dt[1]) == int:
                    if wmid:
                        wmid = int(wmid)
                        dll.SetWmOptionEx(wmid, dt[0], dt[1])
                    else:
                        dll.SetWmOption(dt[0], dt[1])
        else:
            return "error"
    return "success"


@app.route('/ocr/', methods=['POST'])
def ocr(wmid):
    f = request.files.get('img')
    if f is None:
        # 表示没有发送文件
        return "未上传字库"
    data = f.read()
    # return dll.GetImageFromBuffer(data, len(data), pwd)
    res = create_string_buffer(1024)  # 创建文本缓冲区
    if wmid:
        wmid = int(wmid)
        data = dll.GetImageFromBufferEx(wmid, data, len(data), res)
    else:
        data = dll.GetImageFromBuffer(data, len(data), res)
    if data:  # 使用绝对路径
        res = res.raw.decode("GBK").strip()
        print(res)
        return res
    else:
        return ""


@click.command()
@click.option('--port', default=5432, type=int, help='port of websocket server.')
def server(port):
    ser = make_server('0.0.0.0', port, app)
    ser.serve_forever()
    app.run()


if __name__ == '__main__':
    # 启动服务,并指定端口号
    server()

install.bat

@echo off

rem conda的python引擎库名字
set env=test

cd %~dp0
%~d0

conda create -n %env% python=3.7 && conda activate %env% && package.bat %env%
@echo package ok!!
pause

package.bat

@echo off
@echo package start...
echo %1
FOR /F "delims=/ tokens=1" %%i IN ('conda env list ^| find "%1"') DO @set pkg=%%i
SET PADDLEOCR_PATH=%pkg:~25%\Lib\site-packages
echo %PADDLEOCR_PATH%
SET CODE_PATH=%~dp0
echo %CODE_PATH%
cd %~dp0
%~d0

pip install flask
pip install pyinstaller

pyinstaller -F --clean -y -i logo.ico wmcode.py --add-data WmCode.dll;.

@echo package ok!!
pause

uibot 客户端:

# coding=utf-8
'''
Created on 2022年6月9日

@author: 瞌睡蟲子
'''
from time import sleep
import os
import re
from os.path import join, dirname
import requests

ws_port = None


def server(port=5432):
    global ws_port
    ws_port = port
    p = _checkServer()
    if p == 0:
        cmd = "cd /d \"" + join(dirname(__file__), 'wmcodeServer') + \
            "\"&start wmcodeServer.exe --port " + str(port)
        print(cmd)
        os.system(cmd)
    else:
        ws_port = int(p)
    sleep(2)


def _checkServer(pname="wmcodeServer.exe"):
    data = _command("tasklist | findstr " + pname)
    if data == "":
        return 0
    print(data)
    data = re.findall("(\\d+)\\s+Console", data)
    print(data)
    for pid in data:
        temp = _command(
            "netstat -ano | findstr LISTENING | findstr " + pid + "$")
        if len(temp) > 0:
            temp = re.findall(
                "TCP\\s+[\\d.]+:(\\d+)\\s+[\\d.]+:\\d+\\s+LISTENING", temp)
            if len(temp) > 0:
                return int(temp[0])
    return 0


def _command(sCommand):
    with os.popen(sCommand, 'r') as f:
        res = f.read()
    return res


def _do_url(path="load", wmid=""):
    global ws_port
    return "http://127.0.0.1:%d/%s/%s" % (ws_port, path, wmid)


def _load(datPath, password, ex=None):
    global ws_port
    if ws_port == None:
        server()
    resp = requests.post("http://127.0.0.1:%d/load" % (ws_port), files={
        'dat': (datPath, open(datPath, 'rb'))}, data={"password": password, "ex": ex}, timeout=6000, verify=True, allow_redirects=True)
    return int(resp.content.decode())


def _option(OptionIndex, OptionValue, wmid=""):
    resp = requests.post(_do_url("option", wmid), json=[
                         [OptionIndex, OptionValue]], timeout=6000, verify=True, allow_redirects=True)
    return resp.content.decode()


def _ocr(img, wmid=""):
    resp = requests.post(_do_url("ocr", wmid), files={
        'img': (img, open(img, 'rb'))}, timeout=6000, verify=True, allow_redirects=True)
    return resp.content.decode()


def LoadDat(datPath, password):
    return _load(datPath, password)


def getCodeFormFile(img):
    return _ocr(img)


def SetWmOption(OptionIndex, OptionValue):
    return _option(OptionIndex, OptionValue)


def LoadDatEx(datPath, password):
    return _load(datPath, password, "1")


def getCodeFormFileEx(wmid, img):
    return _ocr(img, wmid)


def SetWmOptionEx(wmid, OptionIndex, OptionValue):
    return _option(OptionIndex, OptionValue, wmid)


def Calculator(Expression):
    return eval(Expression)


if __name__ == "__main__":
    server()
    a = LoadDat(r"D:\tools\wmcode\专利网代理师信息\专利网代理师信息.dat", "12345606")
    print(a)
    a = SetWmOption(1, 3)
    print(a)
    vcode = getCodeFormFile(r"C:\Users\44413\Desktop\aaa\freeze.jpg")
    print(vcode)
    print(Calculator("1+2"))

你可能感兴趣的:(完美验证码改web服务)