脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python调用易语言动态链接库实现验证码功能

Python调用易语言动态链接库实现验证码功能

2021-12-21 12:58python可乐编程 Python

今天成功把易语言调用验证码通杀的DLL在Python中成功调用了,心理美滋滋的,接着把我的经验及示例代码分享给大家,希望对大家有所帮助

Python调用易语言动态链接库实现验证码功能

今天成功把易语言调用验证码通杀的dll在python中成功调用了
特此共享出来,下面是识别截图:

Python调用易语言动态链接库实现验证码功能

识别方法1:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""当然在学习python的道路上肯定会困难,没有好的学习资料,怎么去学习呢? 学习python中有不明白推荐加入交流群号:928946953 群里有志同道合的小伙伴,互帮互助, 群里有不错的视频学习教程和pdf!还有大牛解答!"""
# 来源:http://www.sanye.cx/?id=12022
# 优点:载入快、识别速度高、识别精度较高
# 缺点:仅在32位python环境中成功运行
 
 
# 获取上级目录
path = os.path.abspath(os.path.dirname(os.getcwd()))
# 获取验证码文件夹
img_list = os.listdir(path + r"\captcha")
# 载入识别库
dll = cdll.loadlibrary(path + r"\ocr1\ocr.dll")
# 初始化识别库
dll.init()
# 遍历图片并识别
for i in img_list:
    # 读入图片
    with open(path + r"\captcha\{0}".format(i), "rb") as file:
        # 读入图片
        image = file.read()
        # 利用dll中的ocr函数进行识别
        str = dll.ocr(image, len(image))
        # 返回的是指针,所以此处将指针转换为字符串,然后再编码即可得到字符串类型
        text = string_at(str).decode("utf-8")
        print(f"识别返回:{text},类型:{type(text)},id地址:{id(text)}")
识别方法2
 
# 来源:[url=https://www.52pojie.cn/thread-1072587-1-1.html]https://www.52pojie.cn/thread-1072587-1-1.html[/url]
# 优点:识别速度高、识别精度高
# 缺点:仅在32位python环境中成功运行、载入时间较长
 
# 获取上级目录
path = os.path.abspath(os.path.dirname(os.getcwd()))
# 载入识别库
dll = cdll.loadlibrary(path + r"\ocr2\ocrs.dll")
 
# 载入字库与建立字库索引
with open(path + r"\ocr2\通杀英文数字库.cnn", "rb") as file:
    # 载入字库
    word_bank = file.read()
    # 建立字库索引
    work_index = dll.init(path, word_bank, len(word_bank), -1, 1)
# 读入待识别图片列表
img_list = os.listdir(path + "\captcha")
# 循环识别图片并输出
for i in img_list:
    # 打开指定图片
    with open(path + "\captcha\{0}".format(i), "rb") as file_img:
        # 读入图片
        image = file_img.read()
        str = create_string_buffer(100# 创建文本缓冲区
        dll.ocr(work_index, image, len(image), str# 利用dll中的识别函数进行识别
        text = str.raw.decode("utf-8"# 对识别的返回值进行编码
        print(f"识别返回:{text},类型:{type(text)},id地址:{id(text)}")

Python调用易语言动态链接库实现验证码功能

1.自己弄了一个类,下载下来直接使用,调用方法:

?
1
2
3
4
5
dll = ver_code_1(dll文件所在的文件夹目录)
#或者
dll = ver_code_2(dll文件所在的文件夹目录)
#识别图片:
dll.ocr(图片)

2.修正了识别库2空白字符未消除,无法正确判断长度的问题(可以利用固定长度判断是否符合,进行初步筛选,避免提交后网页返回验证码错误)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
from ctypes import *
 
 
class ver_code_1:
    # 启动时需要传入ocr.dll
    def __init__(self, path):
        # 载入识别库
        self.dll = cdll.loadlibrary(path + r"\ocr.dll")
        # 初始化识别库
        self.dll.init()
 
    def ocr(self, image):
        str = self.dll.ocr(image, len(image))
        # 返回的是指针,所以此处将指针转换为字符串,然后再编码即可得到字符串类型
        return string_at(str).decode("utf-8")
 
 
class ver_code_2:
    def __init__(self, path):
        # 载入识别库
        self.dll = cdll.loadlibrary(path + r"\ocrs.dll")
        # 载入字库与建立字库索引
        with open(path + r"\通杀英文数字库.cnn", "rb") as file:
            # 载入字库
            self.word_bank = file.read()
            # 建立字库索引
            self.word_index = self.dll.init(path, self.word_bank, len(self.word_bank), -1, 1)
 
    def ocr(self, image):
        str = create_string_buffer(100# 创建文本缓冲区
        self.dll.ocr(self.word_index, image, len(image), str# 利用dll中的识别函数进行识别
        return str.raw.decode("utf-8").rstrip('\x00'# 对识别的返回值进行编码后返回,这里的\x00是删除缓冲区的空白符

注意!测试环境为:
python 3.9.2 (tags/v3.9.2:1a79785, feb 19 2021, 13:30:23) [msc v.1928 32 bit (intel)] on win32
经测试,无法在64位环境下调用,如有大佬能实现,烦请告知一下

关于dll改64位的思路:
我找到了论坛中的ida pro,成功将dll进行了反编译,如图:

Python调用易语言动态链接库实现验证码功能

其实最关键的就是以上的init以及ocr两个函数,但是后续如何将ida pro项目转换为64位,然后进行编译,目前没有找到合适的方法,如果有大佬麻烦告知一下。

到此这篇关于python调用易语言动态链接库,实现验证码通杀例子的文章就介绍到这了,更多相关python易语言验证码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/pythonQqun200160592/p/15152466.html

延伸 · 阅读

精彩推荐