easy-captcha-python 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ 常量定义
4
+ """
5
+
6
+ # 常用颜色 RGB值
7
+ COLORS = [
8
+ (0, 135, 255),
9
+ (51, 153, 51),
10
+ (255, 102, 102),
11
+ (255, 153, 0),
12
+ (153, 102, 0),
13
+ (153, 102, 153),
14
+ (51, 153, 153),
15
+ (102, 102, 255),
16
+ (0, 102, 204),
17
+ (204, 51, 51),
18
+ (0, 153, 204),
19
+ (0, 51, 102)
20
+ ]
21
+
22
+ # 验证码字符类型
23
+ TYPE_DEFAULT = 1 # 数字和字母混合
24
+ TYPE_ONLY_NUMBER = 2 # 纯数字
25
+ TYPE_ONLY_CHAR = 3 # 纯字母
26
+ TYPE_ONLY_UPPER = 4 # 纯大写字母
27
+ TYPE_ONLY_LOWER = 5 # 纯小写字母
28
+ TYPE_NUM_AND_UPPER = 6 # 数字和大写字母
29
+
30
+ # 内置字体索引
31
+ FONT_1 = 0
32
+ FONT_2 = 1
33
+ FONT_3 = 2
34
+ FONT_4 = 3
35
+ FONT_5 = 4
36
+ FONT_6 = 5
37
+ FONT_7 = 6
38
+ FONT_8 = 7
39
+ FONT_9 = 8
40
+ FONT_10 = 9
41
+
42
+ # 字体文件名
43
+ FONT_NAMES = [
44
+ "actionj.ttf",
45
+ "epilog.ttf",
46
+ "fresnel.ttf",
47
+ "headache.ttf",
48
+ "lexo.ttf",
49
+ "prefix.ttf",
50
+ "progbot.ttf",
51
+ "ransom.ttf",
52
+ "robot.ttf",
53
+ "scandal.ttf"
54
+ ]
55
+
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,445 @@
1
+ Metadata-Version: 2.4
2
+ Name: easy-captcha-python
3
+ Version: 0.1.0
4
+ Summary: Python图形验证码生成库,支持GIF、中文、算术等类型
5
+ Author-email: Kevin Roo <mostvegetableplayer@gmail.com>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://gitee.com/diamondore/EasyCaptcha-Python
8
+ Project-URL: Repository, https://gitee.com/diamondore/EasyCaptcha-Python
9
+ Keywords: captcha,verification code,gif,image,验证码
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.7
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Python: >=3.7
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: Pillow>=9.0.0
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=7.0; extra == "dev"
27
+ Requires-Dist: pytest-cov; extra == "dev"
28
+ Requires-Dist: black; extra == "dev"
29
+ Requires-Dist: flake8; extra == "dev"
30
+ Dynamic: license-file
31
+ Dynamic: requires-python
32
+
33
+ # EasyCaptcha-Python
34
+
35
+ ![Python](https://img.shields.io/badge/python-3.7+-blue.svg)
36
+ ![License](https://img.shields.io/badge/license-Apache%202.0-green.svg)
37
+
38
+ Python 图形验证码生成库,支持 GIF、中文、算术等类型,可用于 Python Web、桌面应用等项目。
39
+
40
+ 这是 [EasyCaptcha](https://github.com/whvcse/EasyCaptcha) Java 版本的 Python 实现。
41
+
42
+ ---
43
+
44
+ ## 1.简介
45
+
46
+ &emsp;Python 图形验证码,支持 gif、中文、算术等类型,可用于 Flask、Django、FastAPI 等 Web 框架。
47
+
48
+ ---
49
+
50
+ ## 2.效果展示
51
+
52
+ **PNG 类型:**
53
+
54
+ ![验证码](https://s2.ax1x.com/2019/08/23/msFrE8.png)
55
+ &emsp;&emsp;
56
+ ![验证码](https://s2.ax1x.com/2019/08/23/msF0DP.png)
57
+ &emsp;&emsp;
58
+ ![验证码](https://s2.ax1x.com/2019/08/23/msFwut.png)
59
+
60
+ **GIF 类型:**
61
+
62
+ ![验证码](https://s2.ax1x.com/2019/08/23/msFzVK.gif)
63
+ &emsp;&emsp;
64
+ ![验证码](https://s2.ax1x.com/2019/08/23/msFvb6.gif)
65
+ &emsp;&emsp;
66
+ ![验证码](https://s2.ax1x.com/2019/08/23/msFXK1.gif)
67
+
68
+ **算术类型:**
69
+
70
+ ![验证码](https://s2.ax1x.com/2019/08/23/mskKPg.png)
71
+ &emsp;&emsp;
72
+ ![验证码](https://s2.ax1x.com/2019/08/23/msknIS.png)
73
+ &emsp;&emsp;
74
+ ![验证码](https://s2.ax1x.com/2019/08/23/mskma8.png)
75
+
76
+ **中文类型:**
77
+
78
+ ![验证码](https://s2.ax1x.com/2019/08/23/mskcdK.png)
79
+ &emsp;&emsp;
80
+ ![验证码](https://s2.ax1x.com/2019/08/23/msk6Z6.png)
81
+ &emsp;&emsp;
82
+ ![验证码](https://s2.ax1x.com/2019/08/23/msksqx.png)
83
+
84
+ **内置字体:**
85
+
86
+ ![验证码](https://s2.ax1x.com/2019/08/23/msAVSJ.png)
87
+ &emsp;&emsp;
88
+ ![验证码](https://s2.ax1x.com/2019/08/23/msAAW4.png)
89
+ &emsp;&emsp;
90
+ ![验证码](https://s2.ax1x.com/2019/08/23/msAkYF.png)
91
+
92
+ ---
93
+
94
+ ## 3.安装
95
+
96
+ ### 3.1.使用 pip 安装
97
+
98
+ ```bash
99
+ pip install easy-captcha-python
100
+ ```
101
+
102
+ ### 3.2.从源码安装
103
+
104
+ ```bash
105
+ git clone https://github.com/yourusername/easy-captcha-python.git
106
+ cd easy-captcha-python
107
+ pip install -e .
108
+ ```
109
+
110
+ ---
111
+
112
+ ## 4.使用方法
113
+
114
+ ### 4.1.快速开始
115
+
116
+ ```python
117
+ from easy_captcha import SpecCaptcha
118
+ from io import BytesIO
119
+
120
+ # 三个参数分别为宽、高、位数
121
+ captcha = SpecCaptcha(130, 48, 5)
122
+
123
+ # 获取验证码文本
124
+ code = captcha.text()
125
+ print(f"验证码: {code}")
126
+
127
+ # 输出到文件
128
+ with open('captcha.png', 'wb') as f:
129
+ stream = BytesIO()
130
+ captcha.out(stream)
131
+ f.write(stream.getvalue())
132
+ ```
133
+
134
+ ### 4.2.在 Flask 中使用
135
+
136
+ ```python
137
+ from flask import Flask, session, make_response
138
+ from easy_captcha import SpecCaptcha
139
+ from io import BytesIO
140
+
141
+ app = Flask(__name__)
142
+ app.secret_key = 'your-secret-key'
143
+
144
+ @app.route('/captcha')
145
+ def captcha():
146
+ # 创建验证码
147
+ cap = SpecCaptcha(130, 48, 5)
148
+ # 验证码文本存入session
149
+ session['captcha'] = cap.text().lower()
150
+
151
+ # 输出图片
152
+ stream = BytesIO()
153
+ cap.out(stream)
154
+
155
+ response = make_response(stream.getvalue())
156
+ response.headers['Content-Type'] = 'image/png'
157
+ return response
158
+
159
+ @app.route('/verify/<code>')
160
+ def verify(code):
161
+ # 获取session中的验证码
162
+ if code.lower() == session.get('captcha'):
163
+ return '验证成功'
164
+ return '验证失败'
165
+ ```
166
+
167
+ 前端 HTML 代码:
168
+
169
+ ```html
170
+ <img src="/captcha" width="130px" height="48px" />
171
+ ```
172
+
173
+ ### 4.3.在 Django 中使用
174
+
175
+ ```python
176
+ from django.http import HttpResponse
177
+ from easy_captcha import SpecCaptcha
178
+ from io import BytesIO
179
+
180
+ def captcha(request):
181
+ cap = SpecCaptcha(130, 48, 5)
182
+ # 验证码文本存入session
183
+ request.session['captcha'] = cap.text().lower()
184
+
185
+ # 输出图片
186
+ stream = BytesIO()
187
+ cap.out(stream)
188
+
189
+ return HttpResponse(stream.getvalue(), content_type='image/png')
190
+ ```
191
+
192
+ ### 4.4.在 FastAPI 中使用
193
+
194
+ ```python
195
+ from fastapi import FastAPI, Response
196
+ from easy_captcha import SpecCaptcha
197
+ from io import BytesIO
198
+
199
+ app = FastAPI()
200
+
201
+ @app.get("/captcha")
202
+ async def captcha():
203
+ cap = SpecCaptcha(130, 48, 5)
204
+ code = cap.text()
205
+
206
+ stream = BytesIO()
207
+ cap.out(stream)
208
+
209
+ return Response(content=stream.getvalue(), media_type="image/png")
210
+ ```
211
+
212
+ ### 4.5.前后端分离项目
213
+
214
+ &emsp;前后端分离项目建议使用 base64 编码返回:
215
+
216
+ ```python
217
+ from flask import Flask, jsonify, request
218
+ import uuid
219
+
220
+ app = Flask(__name__)
221
+ # 这里使用字典模拟,生产环境建议使用Redis
222
+ captcha_store = {}
223
+
224
+ @app.route('/captcha')
225
+ def get_captcha():
226
+ from easy_captcha import SpecCaptcha
227
+
228
+ cap = SpecCaptcha(130, 48, 5)
229
+ code = cap.text().lower()
230
+ key = str(uuid.uuid4())
231
+
232
+ # 存储验证码(生产环境建议存到Redis并设置过期时间)
233
+ captcha_store[key] = code
234
+
235
+ # 返回key和base64图片
236
+ return jsonify({
237
+ 'key': key,
238
+ 'image': cap.to_base64()
239
+ })
240
+
241
+ @app.route('/login', methods=['POST'])
242
+ def login():
243
+ data = request.json
244
+ ver_key = data.get('verKey')
245
+ ver_code = data.get('verCode', '').lower()
246
+
247
+ # 验证验证码
248
+ if ver_code == captcha_store.get(ver_key):
249
+ # 验证成功后删除
250
+ captcha_store.pop(ver_key, None)
251
+ return jsonify({'success': True})
252
+ return jsonify({'success': False, 'message': '验证码错误'})
253
+ ```
254
+
255
+ 前端使用示例:
256
+
257
+ ```html
258
+ <img id="verImg" width="130px" height="48px" />
259
+
260
+ <script>
261
+ var verKey;
262
+ // 获取验证码
263
+ fetch("/captcha")
264
+ .then((res) => res.json())
265
+ .then((data) => {
266
+ verKey = data.key;
267
+ document.getElementById("verImg").src = data.image;
268
+ });
269
+
270
+ // 登录
271
+ fetch("/login", {
272
+ method: "POST",
273
+ headers: { "Content-Type": "application/json" },
274
+ body: JSON.stringify({
275
+ verKey: verKey,
276
+ verCode: "8u6h",
277
+ username: "admin",
278
+ password: "admin",
279
+ }),
280
+ })
281
+ .then((res) => res.json())
282
+ .then((data) => console.log(data));
283
+ </script>
284
+ ```
285
+
286
+ ---
287
+
288
+ ## 5.更多设置
289
+
290
+ ### 5.1.验证码类型
291
+
292
+ ```python
293
+ from easy_captcha import (
294
+ SpecCaptcha, GifCaptcha, ChineseCaptcha,
295
+ ChineseGifCaptcha, ArithmeticCaptcha
296
+ )
297
+
298
+ # PNG类型
299
+ captcha = SpecCaptcha(130, 48, 5)
300
+ code = captcha.text() # 获取验证码文本
301
+ chars = captcha.text_char() # 获取验证码字符数组
302
+
303
+ # GIF类型
304
+ captcha = GifCaptcha(130, 48, 5)
305
+
306
+ # 中文类型
307
+ captcha = ChineseCaptcha(130, 48, 4)
308
+
309
+ # 中文GIF类型
310
+ captcha = ChineseGifCaptcha(130, 48, 4)
311
+
312
+ # 算术类型
313
+ captcha = ArithmeticCaptcha(130, 48, 2)
314
+ captcha.len = 3 # 几位数运算,默认是两位
315
+ formula = captcha.get_arithmetic_string() # 获取运算公式:3+2=?
316
+ result = captcha.text() # 获取运算结果:5
317
+
318
+ # 输出验证码
319
+ from io import BytesIO
320
+ stream = BytesIO()
321
+ captcha.out(stream)
322
+ ```
323
+
324
+ > 注意:<br/> > &emsp;算术验证码的 len 表示是几位数运算,而其他验证码的 len 表示验证码的位数,算术验证码的 text()表示的是公式的结果,
325
+ > 对于算术验证码,你应该把公式的结果存储到 session,而不是公式。
326
+
327
+ ### 5.2.验证码字符类型
328
+
329
+ | 类型 | 描述 |
330
+ | :----------------- | :------------- |
331
+ | TYPE_DEFAULT | 数字和字母混合 |
332
+ | TYPE_ONLY_NUMBER | 纯数字 |
333
+ | TYPE_ONLY_CHAR | 纯字母 |
334
+ | TYPE_ONLY_UPPER | 纯大写字母 |
335
+ | TYPE_ONLY_LOWER | 纯小写字母 |
336
+ | TYPE_NUM_AND_UPPER | 数字和大写字母 |
337
+
338
+ 使用方法:
339
+
340
+ ```python
341
+ from easy_captcha import SpecCaptcha, TYPE_ONLY_NUMBER
342
+
343
+ captcha = SpecCaptcha(130, 48, 5)
344
+ captcha.char_type = TYPE_ONLY_NUMBER
345
+ ```
346
+
347
+ > 只有`SpecCaptcha`和`GifCaptcha`设置才有效果。
348
+
349
+ ### 5.3.字体设置
350
+
351
+ 内置字体:
352
+
353
+ | 字体 | 效果 |
354
+ | :------ | :--------------------------------------------- |
355
+ | FONT_1 | ![](https://s2.ax1x.com/2019/08/23/msMe6U.png) |
356
+ | FONT_2 | ![](https://s2.ax1x.com/2019/08/23/msMAf0.png) |
357
+ | FONT_3 | ![](https://s2.ax1x.com/2019/08/23/msMCwj.png) |
358
+ | FONT_4 | ![](https://s2.ax1x.com/2019/08/23/msM9mQ.png) |
359
+ | FONT_5 | ![](https://s2.ax1x.com/2019/08/23/msKz6S.png) |
360
+ | FONT_6 | ![](https://s2.ax1x.com/2019/08/23/msKxl8.png) |
361
+ | FONT_7 | ![](https://s2.ax1x.com/2019/08/23/msMPTs.png) |
362
+ | FONT_8 | ![](https://s2.ax1x.com/2019/08/23/msMmXF.png) |
363
+ | FONT_9 | ![](https://s2.ax1x.com/2019/08/23/msMVpV.png) |
364
+ | FONT_10 | ![](https://s2.ax1x.com/2019/08/23/msMZlT.png) |
365
+
366
+ 使用方法:
367
+
368
+ ```python
369
+ from easy_captcha import SpecCaptcha, FONT_1, FONT_2
370
+
371
+ captcha = SpecCaptcha(130, 48, 5)
372
+
373
+ # 设置内置字体
374
+ captcha.set_font(FONT_1, size=32)
375
+
376
+ # 也可以使用系统字体(需要PIL.ImageFont支持)
377
+ from PIL import ImageFont
378
+ captcha._font = ImageFont.truetype("arial.ttf", 32)
379
+ ```
380
+
381
+ ### 5.4.输出 base64 编码
382
+
383
+ ```python
384
+ from easy_captcha import SpecCaptcha
385
+
386
+ captcha = SpecCaptcha(130, 48, 5)
387
+ base64_str = captcha.to_base64()
388
+
389
+ # 如果不想要base64的头部data:image/png;base64,
390
+ base64_str = captcha.to_base64("") # 加一个空的参数即可
391
+ ```
392
+
393
+ ### 5.5.输出到文件
394
+
395
+ ```python
396
+ from easy_captcha import SpecCaptcha
397
+ from io import BytesIO
398
+
399
+ captcha = SpecCaptcha(130, 48, 5)
400
+
401
+ # 输出到文件
402
+ with open('captcha.png', 'wb') as f:
403
+ stream = BytesIO()
404
+ captcha.out(stream)
405
+ f.write(stream.getvalue())
406
+ ```
407
+
408
+ ---
409
+
410
+ ## 6.完整示例
411
+
412
+ 查看 `examples/` 目录获取更多示例:
413
+
414
+ - `basic_usage.py` - 基本使用示例
415
+ - `all_types_demo.py` - 所有验证码类型演示
416
+
417
+ 运行示例:
418
+
419
+ ```bash
420
+ # 运行基本示例
421
+ python examples/basic_usage.py
422
+
423
+ # 运行完整演示
424
+ python examples/all_types_demo.py
425
+ ```
426
+
427
+ 所有生成的验证码图片将保存到 `./out/` 目录。
428
+
429
+ ---
430
+
431
+ ## 7.许可证
432
+
433
+ Apache License 2.0
434
+
435
+ ---
436
+
437
+ ## 8.致谢
438
+
439
+ 本项目是 [EasyCaptcha](https://github.com/whvcse/EasyCaptcha) 的 Python 实现版本。
440
+
441
+ ---
442
+
443
+ ## 9.贡献
444
+
445
+ 欢迎提交 Issue 和 Pull Request!
@@ -0,0 +1,28 @@
1
+ easy_captcha/__init__.py,sha256=9pD-Bn3mDHOKQO8Tl3twXeuXnfV_sPlai8cYDn3BX28,1049
2
+ easy_captcha/constants.py,sha256=EGRwpINfXVWF-9MbouUlZAzTK2nLqlD133rX8gLm6WM,980
3
+ easy_captcha/base/__init__.py,sha256=WgOokF66LZc1f0kOJBkwRAUH9NvIFw3lghxlQs213kg,360
4
+ easy_captcha/base/arithmetic_captcha_abstract.py,sha256=1j5srYkVQlZhZNaHOwZdWvFQVw93qtk7AmFLzWmTbgw,2327
5
+ easy_captcha/base/captcha.py,sha256=zo1HWyVhpmVoXJiFb4aHLUom6SGrdqQR9km5SVIe_8Q,10017
6
+ easy_captcha/base/chinese_captcha_abstract.py,sha256=tHBl87LNKbk19NMLrzFj68y6dlTd6CdrebnbLsK1kx8,4060
7
+ easy_captcha/base/randoms.py,sha256=aQ7Mu44qM9ndYUtmMqeX_MFydk7hcdZu_noAIiCm-IY,2612
8
+ easy_captcha/captcha/__init__.py,sha256=af9DUIkh-RFGiuoK_92lCVZKtINMh9g0RKI1497UEfU,417
9
+ easy_captcha/captcha/arithmetic_captcha.py,sha256=QPNyX8KCYRhbT173uErL8x4txbGgsVQxXN6COPJsnBc,3240
10
+ easy_captcha/captcha/chinese_captcha.py,sha256=mCh01efw1VT_zPDb1zZQg8q5iCShMJR4Bhms1qasOa4,3230
11
+ easy_captcha/captcha/chinese_gif_captcha.py,sha256=Sgn9_KnfNszgOCVtmsM3DGNPwKnmBAsyrjaYrcODAPI,5547
12
+ easy_captcha/captcha/gif_captcha.py,sha256=cxmu85R7YfvZkZkneWAjLBJmcmF8FHzZTGaEQqR8mNY,5493
13
+ easy_captcha/captcha/spec_captcha.py,sha256=DYmgQ6ubqBubiYFpT-K8EKWzMWJYrnMChpVOWXm-snI,3188
14
+ easy_captcha/fonts/actionj.ttf,sha256=VEQI9i-3woTFkvtaSG7W0NAGItzY97vRK0DWnMrYCPc,34944
15
+ easy_captcha/fonts/epilog.ttf,sha256=ErdQS9dU2i67OpO9vZr3r4ZYCxDA8o8I5D63uaQIAgg,30136
16
+ easy_captcha/fonts/fresnel.ttf,sha256=rfJdx1BDyHrDehvGK-olPasYVDvTWZmuviRKf1SRj7c,75560
17
+ easy_captcha/fonts/headache.ttf,sha256=Tab_L454Hbocf7GSaJuzCaNX4mhEDHIvGXhNJF3bg8w,45292
18
+ easy_captcha/fonts/lexo.ttf,sha256=mjChjEkU0eOUwZnSZw36DfgzWXaxjgneQ4gWeE_S53M,77600
19
+ easy_captcha/fonts/prefix.ttf,sha256=u3pwW39SNi2XnY4eTn6_JuCCd6bMaVWzvZ9Yt13mCHE,99016
20
+ easy_captcha/fonts/progbot.ttf,sha256=cvB-KMqU5qt2jO8Qzqrel8g-BSaf40N3L4Eyb3-ia_E,86956
21
+ easy_captcha/fonts/ransom.ttf,sha256=v0BkWa6YuLnJ93D2z1FvY1r6wjTsNO9Ygj79i3NzdZM,35812
22
+ easy_captcha/fonts/robot.ttf,sha256=Wup1XBhJsusoJoYshAnMbyB4pLP3rPR0Bf6GmNOgbiQ,45072
23
+ easy_captcha/fonts/scandal.ttf,sha256=uXDDqJKqLc41IOFUH62OB1U-cCp1pdPqwDsNkm8lvbY,41320
24
+ easy_captcha_python-0.1.0.dist-info/licenses/LICENSE,sha256=gcuuhKKc5-dwvyvHsXjlC9oM6N5gZ6umYbC8ewW1Yvg,35821
25
+ easy_captcha_python-0.1.0.dist-info/METADATA,sha256=aDWsXiVOzvOzc4GkW-BPPlkWWBUFpouL43Hd7sAfnJ0,11340
26
+ easy_captcha_python-0.1.0.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
27
+ easy_captcha_python-0.1.0.dist-info/top_level.txt,sha256=Xw8j3C2lzHMkbiHr_3frpjIy0C8XPn86WecmcS9kicc,13
28
+ easy_captcha_python-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+