图形验证码处理

import base64
import json

import requests
import uuid


class ImageCode:
    # 调用图片验证码原有接口获取编码
    def get_image(self, uuid):
        url = "http://shop.lemonban.com:8108/captcha.jpg"
        data = {"uuid": uuid}
        # get请求对表格进行传参(params)
        response = requests.get(url=url, params=data)
        print(response.content)
        # response.content:以字节为单位返回响应的内容
        image_byte = response.content
        # 保持图片验证码
        with open(file="code.jpg", mode="wb") as file:
            file.write(image_byte)
        # 打开图片验证码进行解密
        with open(file="code.jpg", mode="rb") as image_file:
            image_data = image_file.read()
        # 解码方法,image_byte:字符串
        base64_data = base64.b64encode(image_data)
        # 编码的数据进行解码
        b64 = base64_data.decode()
        return b64

    # 调用图鉴第三方接口进行验证码解密
    def get_image_code(self, uuid):
        b64 = self.get_image(uuid=uuid)
        url = "http://api.ttshitu.com/predict"
        data = {
            "username": "",
            "password": "",
            "typeid": 3,
            "image": b64,  # 图片的base64怎么来
        }
        result = json.loads(requests.post(url=url, json=data).text)
        print("识别结果:", result)
        if result["success"]:
            return result["data"]["result"]
        else:
            return result["message"]


if __name__ == '__main__':
    test = ImageCode()
    test.get_image_code(str(uuid.uuid4()))

 

posted on 2024-04-08 13:18  诚实的表达自己  阅读(17)  评论(0编辑  收藏  举报