go-captcha-py 0.1.0__tar.gz

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.
Files changed (77) hide show
  1. go_captcha_py-0.1.0/LICENSE +201 -0
  2. go_captcha_py-0.1.0/PKG-INFO +441 -0
  3. go_captcha_py-0.1.0/README.md +411 -0
  4. go_captcha_py-0.1.0/pyproject.toml +94 -0
  5. go_captcha_py-0.1.0/pyproject.toml.orig +91 -0
  6. go_captcha_py-0.1.0/src/go_captcha_py/__init__.py +29 -0
  7. go_captcha_py-0.1.0/src/go_captcha_py/base/__init__.py +34 -0
  8. go_captcha_py-0.1.0/src/go_captcha_py/base/helper.py +77 -0
  9. go_captcha_py-0.1.0/src/go_captcha_py/base/imagedata.py +87 -0
  10. go_captcha_py-0.1.0/src/go_captcha_py/base/option.py +44 -0
  11. go_captcha_py-0.1.0/src/go_captcha_py/base/randgen.py +101 -0
  12. go_captcha_py-0.1.0/src/go_captcha_py/cli.py +45 -0
  13. go_captcha_py-0.1.0/src/go_captcha_py/click/__init__.py +98 -0
  14. go_captcha_py-0.1.0/src/go_captcha_py/click/builder.py +142 -0
  15. go_captcha_py-0.1.0/src/go_captcha_py/click/captcha.py +405 -0
  16. go_captcha_py-0.1.0/src/go_captcha_py/click/dot.py +55 -0
  17. go_captcha_py-0.1.0/src/go_captcha_py/click/draw.py +409 -0
  18. go_captcha_py-0.1.0/src/go_captcha_py/click/options.py +296 -0
  19. go_captcha_py-0.1.0/src/go_captcha_py/click/validate.py +22 -0
  20. go_captcha_py-0.1.0/src/go_captcha_py/resources/__init__.py +158 -0
  21. go_captcha_py-0.1.0/src/go_captcha_py/resources/chars.py +3508 -0
  22. go_captcha_py-0.1.0/src/go_captcha_py/resources/fonts/fzshengsksjw_cu.ttf +0 -0
  23. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-1.jpg +0 -0
  24. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-10.jpg +0 -0
  25. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-11.jpg +0 -0
  26. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-12.jpg +0 -0
  27. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-13.jpg +0 -0
  28. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-14.jpg +0 -0
  29. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-15.jpg +0 -0
  30. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-16.jpg +0 -0
  31. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-2.jpg +0 -0
  32. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-3.jpg +0 -0
  33. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-4.jpg +0 -0
  34. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-5.jpg +0 -0
  35. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-6.jpg +0 -0
  36. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-7.jpg +0 -0
  37. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-8.jpg +0 -0
  38. go_captcha_py-0.1.0/src/go_captcha_py/resources/images/image-v2-9.jpg +0 -0
  39. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-1.png +0 -0
  40. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-10.png +0 -0
  41. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-11.png +0 -0
  42. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-12.png +0 -0
  43. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-13.png +0 -0
  44. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-2.png +0 -0
  45. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-3.png +0 -0
  46. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-4.png +0 -0
  47. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-5.png +0 -0
  48. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-6.png +0 -0
  49. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-7.png +0 -0
  50. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-8.png +0 -0
  51. go_captcha_py-0.1.0/src/go_captcha_py/resources/shapes/shape-9.png +0 -0
  52. go_captcha_py-0.1.0/src/go_captcha_py/resources/thumbs/thumb-1.jpg +0 -0
  53. go_captcha_py-0.1.0/src/go_captcha_py/resources/thumbs/thumb-2.jpg +0 -0
  54. go_captcha_py-0.1.0/src/go_captcha_py/resources/thumbs/thumb-3.jpg +0 -0
  55. go_captcha_py-0.1.0/src/go_captcha_py/resources/thumbs/thumb-4.jpg +0 -0
  56. go_captcha_py-0.1.0/src/go_captcha_py/resources/thumbs/thumb-5.jpg +0 -0
  57. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-1-mask.png +0 -0
  58. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-1-shadow.png +0 -0
  59. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-1.png +0 -0
  60. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-2-mask.png +0 -0
  61. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-2-shadow.png +0 -0
  62. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-2.png +0 -0
  63. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-3-mask.png +0 -0
  64. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-3-shadow.png +0 -0
  65. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-3.png +0 -0
  66. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-4-mask.png +0 -0
  67. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-4-shadow.png +0 -0
  68. go_captcha_py-0.1.0/src/go_captcha_py/resources/tiles/tile-4.png +0 -0
  69. go_captcha_py-0.1.0/src/go_captcha_py/rotate/__init__.py +313 -0
  70. go_captcha_py-0.1.0/src/go_captcha_py/slide/__init__.py +75 -0
  71. go_captcha_py-0.1.0/src/go_captcha_py/slide/block.py +30 -0
  72. go_captcha_py-0.1.0/src/go_captcha_py/slide/builder.py +126 -0
  73. go_captcha_py-0.1.0/src/go_captcha_py/slide/captcha.py +275 -0
  74. go_captcha_py-0.1.0/src/go_captcha_py/slide/draw.py +134 -0
  75. go_captcha_py-0.1.0/src/go_captcha_py/slide/options.py +132 -0
  76. go_captcha_py-0.1.0/src/go_captcha_py/slide/validate.py +21 -0
  77. go_captcha_py-0.1.0/src/go_captcha_py/store/__init__.py +115 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [2021] [Awen <wengaolng@gmail.com>]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,441 @@
1
+ Metadata-Version: 2.4
2
+ Name: go-captcha-py
3
+ Version: 0.1.0
4
+ Summary: Python implementation of GoCaptcha (behavior captcha: click / slide / drag-drop / rotate), compatible with go-captcha frontends (Vue/React/Angular/Svelte/Solid/JS)
5
+ Keywords: captcha,behavior-captcha,verification,fastapi,gocaptcha
6
+ Author: choococo
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Security
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Requires-Dist: pillow>=10.0
21
+ Requires-Dist: fastapi>=0.115 ; extra == 'fastapi'
22
+ Requires-Dist: uvicorn>=0.30 ; extra == 'fastapi'
23
+ Requires-Python: >=3.10
24
+ Project-URL: Homepage, https://github.com/choococo/go-captcha-py
25
+ Project-URL: Repository, https://github.com/choococo/go-captcha-py
26
+ Project-URL: Issues, https://github.com/choococo/go-captcha-py/issues
27
+ Project-URL: Upstream, https://github.com/wenlng/go-captcha
28
+ Provides-Extra: fastapi
29
+ Description-Content-Type: text/markdown
30
+
31
+ # go-captcha-py
32
+
33
+ 🧩 **Python 行为验证码库** —— [GoCaptcha](https://github.com/wenlng/go-captcha) 的 Python 复现版本
34
+
35
+ 支持 **点击(文字/图形)**、**滑动拼图**、**拖拽拼图**、**旋转** 四种行为验证码的生成与校验,与 GoCaptcha 官方前端组件(Vue / React / Angular / Svelte / Solid / JavaScript)**协议级兼容**,并内置全部素材资源,安装即用。
36
+
37
+ ![captcha types](docs/images/vue-demo.png)
38
+
39
+ | 点击验证码(文字,主图 + 缩略图) | 点击验证码(图形) | 滑动拼图 | 旋转 |
40
+ | --- | --- | --- | --- |
41
+ | ![click-showcase](docs/images/click-showcase.jpg) | ![click-shape](docs/images/click-shape-master.jpg) | ![slide](docs/images/slide-master.jpg) | ![rotate](docs/images/rotate-master.png) |
42
+
43
+ ## 特性
44
+
45
+ - **四种验证码类型**:Click(Text / Shape)、Slide(Basic / Drag-Drop)、Rotate,生成逻辑与 Go 版对齐
46
+ - **内置素材**:16 张主图、13 个图形、5 张缩略图、4 套拼图 tile、中文字体,全部打包进 wheel
47
+ - **3498 字汉字库**:内置上游完整常用字集,每次生成完全随机;另有字母 / 字母数字混合预设
48
+ - **可读性保障**:文字仅做随机旋转(BICUBIC 抗锯齿,无透视变形),且自动检测与背景的亮度对比,低对比时在调色板及黑白回退色中重选——杜绝"看不清的字"
49
+ - **协议兼容**:`Dot` / `Block` 字段名与 Go 版 JSON tag 一致,直接对接 go-captcha-vue@^1 等官方前端
50
+ - **校验容差**:`click.Validate` / `slide.Validate` / `rotate.Validate` 与 Go 版语义一致
51
+ - **纯 Python 依赖**:仅依赖 [Pillow](https://python-pillow.org/),支持 **Python 3.10+**(3.10 / 3.13 实测通过),无 CGO / 无系统库
52
+ - **可嵌入**:核心库零 Web 框架依赖,可集成进 FastAPI / Flask / Django / gRPC 等任意后端
53
+
54
+ ## 安装
55
+
56
+ **pip:**
57
+
58
+ ```bash
59
+ pip install go-captcha-py
60
+ ```
61
+
62
+ > 也可以直接从 GitHub 安装开发版本:`pip install git+https://github.com/choococo/go-captcha-py.git`
63
+
64
+ **uv:**
65
+
66
+ ```bash
67
+ uv add go-captcha-py
68
+ ```
69
+
70
+ **从本仓库(开发模式):**
71
+
72
+ ```bash
73
+ git clone https://github.com/choococo/go-captcha-py.git
74
+ cd go-captcha-py
75
+ uv sync --extra fastapi # 或只装核心: uv sync
76
+ ```
77
+
78
+ ## 快速开始
79
+
80
+ ### 1. 生成一个点击验证码(文字模式)
81
+
82
+ ```python
83
+ from go_captcha_py import click, store
84
+
85
+ capt = click.make_default_text_captcha() # 使用内置素材
86
+ data = capt.generate()
87
+
88
+ # 答案保存在服务端(不要发给前端!)
89
+ captcha_id = store.gen_key()
90
+ mem_store = store.MemoryStore(ttl=600)
91
+ mem_store.set(captcha_id, data.get_data())
92
+
93
+ # 图片以 base64 发给前端
94
+ payload = {
95
+ "captcha_id": captcha_id,
96
+ "image_base64": data.get_master_image().to_base64(), # JPEG 主图
97
+ "thumb_base64": data.get_thumb_image().to_base64(), # PNG 缩略图
98
+ }
99
+ ```
100
+
101
+ ### 2. 校验用户点击
102
+
103
+ 前端回传用户点击的坐标点(`dots`),与保存的答案比对:
104
+
105
+ ```python
106
+ from go_captcha_py import click
107
+
108
+ answer = mem_store.pop(captcha_id) # 一次性消费
109
+ ok = all(
110
+ click.validate(sx, sy, dot.x, dot.y, dot.width, dot.height, padding=5)
111
+ for (sx, sy), dot in zip(user_dots, answer.values())
112
+ )
113
+ ```
114
+
115
+ ### 3. 滑动 / 拖拽 / 旋转
116
+
117
+ ```python
118
+ from go_captcha_py import slide, rotate
119
+
120
+ # 滑动拼图(basic:滑块固定 Y 轴)
121
+ data = slide.make_default_captcha().generate()
122
+ block = data.get_data() # {'x': 202, 'y': 147, 'width': 67, ...}
123
+ ok = slide.validate(sx, sy, block.x, block.y, padding=3)
124
+
125
+ # 拖拽拼图(drag-drop:自由 X/Y)
126
+ data = slide.make_default_drag_drop_captcha().generate()
127
+
128
+ # 旋转验证码
129
+ data = rotate.make_default_captcha().generate()
130
+ block = data.get_data() # block.angle 为目标角度
131
+ ok = rotate.validate(user_angle, block.angle, padding=6)
132
+ ```
133
+
134
+ ### 4. 图形点击模式
135
+
136
+ ```python
137
+ from go_captcha_py import click
138
+
139
+ capt = click.make_default_shape_captcha() # 13 个内置图形素材
140
+ data = capt.generate()
141
+ ```
142
+
143
+ 图形模式与文字模式使用相同的点击流程:缩略图显示需要依次点击的图形,主图显示随机分布的全部图形,后端仍然通过 `data.get_data()` 保存并校验目标坐标。
144
+
145
+ ![图形点击主图](docs/images/click-shape-master.jpg)
146
+ ![图形点击目标缩略图](docs/images/click-shape-thumb.png)
147
+
148
+ FastAPI 示例中通过查询参数切换到图形模式:
149
+
150
+ ```text
151
+ GET /captcha/click?shape=true
152
+ ```
153
+
154
+ ## FastAPI 集成示例
155
+
156
+ 完整可运行示例见 [`examples/fastapi_server.py`](examples/fastapi_server.py):
157
+
158
+ ```bash
159
+ uv run --extra fastapi uvicorn examples.fastapi_server:app --port 9000 --reload
160
+ ```
161
+
162
+ | 接口 | 说明 |
163
+ | --- | --- |
164
+ | `GET /captcha/click?shape=false` | 生成点击验证码(`shape=true` 为图形模式) |
165
+ | `POST /captcha/click/verify` | 校验点击坐标(`dots=x1,y1;x2,y2`) |
166
+ | `GET /captcha/slide?drag=false` | 生成滑动/拖拽拼图 |
167
+ | `POST /captcha/slide/verify` | 校验滑块位置(`point=x,y`) |
168
+ | `GET /captcha/rotate` | 生成旋转验证码 |
169
+ | `POST /captcha/rotate/verify` | 校验旋转角度(`angle=deg`) |
170
+
171
+ ## 前端对接
172
+
173
+ 官方前端组件库与本库生成的数据格式**完全兼容**:
174
+
175
+ | 框架 | 包名 | 示例 | 状态 |
176
+ | --- | --- | --- | --- |
177
+ | Vue 2 | [`go-captcha-vue@^1`](https://www.npmjs.com/package/go-captcha-vue) | [`examples/vue-demo`](examples/vue-demo) | ✅ 已验证(四种验证码 E2E 4/4 通过) |
178
+ | React | [`go-captcha-react`](https://www.npmjs.com/package/go-captcha-react) | [`examples/multi-framework/react-demo`](examples/multi-framework/react-demo) | ✅ 已验证 |
179
+ | Svelte | [`go-captcha-svelte`](https://www.npmjs.com/package/go-captcha-svelte) | [`examples/multi-framework/svelte-demo`](examples/multi-framework/svelte-demo) | ✅ 已验证 |
180
+ | Solid | [`go-captcha-solid`](https://www.npmjs.com/package/go-captcha-solid) | [`examples/multi-framework/solid-demo`](examples/multi-framework/solid-demo) | ✅ 已验证 |
181
+ | JavaScript | [`go-captcha-jslib`](https://www.npmjs.com/package/go-captcha-jslib) | [`examples/multi-framework/js-demo`](examples/multi-framework/js-demo) | ✅ 已验证 |
182
+ | Angular | [`go-captcha-angular`](https://www.npmjs.com/package/go-captcha-angular) | [`examples/multi-framework/angular-demo`](examples/multi-framework/angular-demo) | ✅ 已验证 |
183
+
184
+ 各前端组件的 data / events 接口完全一致(`image/thumb` + `confirm` 回调),仅组件注册方式不同。
185
+
186
+ ### 一键启动前后端联调(推荐)
187
+
188
+ [`dev.sh`](dev.sh) 同时拉起 FastAPI 后端 + 任一前端 demo,统一日志、Ctrl-C 一起退出(首次自动 `npm install`):
189
+
190
+ ```bash
191
+ ./dev.sh # 后端 + Vue demo(默认)
192
+ ./dev.sh react # 后端 + React demo
193
+ ./dev.sh svelte # 后端 + Svelte demo
194
+ ./dev.sh solid # 后端 + Solid demo
195
+ ./dev.sh js # 后端 + 原生 JS demo
196
+ ./dev.sh angular # 后端 + Angular demo(自动切 nvm node v22)
197
+ ./dev.sh backend # 仅后端
198
+ ```
199
+
200
+ | 前端 | 页面地址 |
201
+ | --- | --- |
202
+ | Vue | http://localhost:5173 |
203
+ | React | http://localhost:5174 |
204
+ | Svelte | http://localhost:5181 |
205
+ | Solid | http://localhost:5182 |
206
+ | 原生 JS | http://localhost:5183 |
207
+ | Angular | http://localhost:4200 |
208
+
209
+ 后端固定 http://127.0.0.1:9000(Swagger 交互文档:http://127.0.0.1:9000/docs,可脱离前端直接调接口)。日志在 `/tmp/gocaptcha-py-logs/{backend,frontend}.log`。
210
+
211
+ ### Docker 运行 FastAPI 示例
212
+
213
+ 只启动后端 API 时,可以使用仓库自带的 Docker 配置:
214
+
215
+ ```bash
216
+ docker compose up --build
217
+ ```
218
+
219
+ 服务地址为 `http://localhost:9000`,Swagger 地址为 `http://localhost:9000/docs`。容器默认关闭 `GOCAPTCHA_DEBUG`,不会把验证码答案返回给客户端;该变量只适合本地自动化测试使用。
220
+
221
+ 等价方式:`make dev` / `make dev-react` / `make backend`(详见 `make help`)。
222
+
223
+ ### 手动分终端启动
224
+
225
+ ```bash
226
+ # 终端 1:后端(加 GOCAPTCHA_DEBUG=1 可在生成接口里带出答案,供自动化 E2E 用)
227
+ GOCAPTCHA_DEBUG=1 uv run --extra fastapi uvicorn examples.fastapi_server:app --port 9000
228
+
229
+ # 终端 2:前端(任选,均需先 npm install)
230
+ cd examples/vue-demo && npm run dev # Vue 2, :5173
231
+ cd examples/multi-framework/react-demo && npm run dev # React, :5174
232
+ cd examples/multi-framework/svelte-demo && npm run dev # Svelte, :5181
233
+ cd examples/multi-framework/solid-demo && npm run dev # Solid, :5182
234
+ cd examples/multi-framework/js-demo && npm run dev # 原生 JS, :5183
235
+ cd examples/multi-framework/angular-demo/angular-app && npx ng serve # Angular, :4200(需 node>=20)
236
+ ```
237
+
238
+ ## API 概览
239
+
240
+ ### `go_captcha_py.click`
241
+
242
+ | 成员 | 说明 |
243
+ | --- | --- |
244
+ | `make_default_text_captcha()` | 内置素材文字点击验证码 |
245
+ | `make_default_shape_captcha()` | 内置素材图形点击验证码 |
246
+ | `make_text_captcha(chars=..., backgrounds=..., ...)` | 自定义素材文字验证码(未指定项回退内置) |
247
+ | `make_shape_captcha(shapes=..., backgrounds=..., ...)` | 自定义素材图形验证码 |
248
+ | `click.validate(sx, sy, dx, dy, w, h, padding)` | 点击命中校验 |
249
+ | `Builder` / `new_builder(...)` | 链式构建(完全控制选项/素材) |
250
+
251
+ ### `go_captcha_py.slide`
252
+
253
+ | 成员 | 说明 |
254
+ | --- | --- |
255
+ | `make_default_captcha()` | 滑动拼图(固定 Y) |
256
+ | `make_default_drag_drop_captcha()` | 拖拽拼图(自由 X/Y) |
257
+ | `make_captcha(backgrounds=..., graph_images=..., ...)` | 自定义素材滑动拼图 |
258
+ | `make_drag_drop_captcha(...)` | 自定义素材拖拽拼图 |
259
+ | `slide.validate(sx, sy, dx, dy, padding)` | 位置命中校验 |
260
+
261
+ ### `go_captcha_py.rotate`
262
+
263
+ | 成员 | 说明 |
264
+ | --- | --- |
265
+ | `make_default_captcha()` | 旋转验证码 |
266
+ | `make_captcha(images=..., ...)` | 自定义背景旋转验证码 |
267
+ | `rotate.validate(src_angle, angle, padding)` | 角度命中校验 |
268
+
269
+ ### `go_captcha_py.store`
270
+
271
+ | 成员 | 说明 |
272
+ | --- | --- |
273
+ | `MemoryStore(ttl)` | 进程内 TTL 缓存(单 worker) |
274
+ | `Store` (Protocol) | 自定义存储接口(Redis 等分布式场景) |
275
+ | `gen_key()` | 生成验证码 id |
276
+
277
+ ### 自定义素材与选项
278
+
279
+ **方式一:便捷工厂(推荐)** —— 未指定的素材自动回退到内置资源:
280
+
281
+ ```python
282
+ from PIL import Image
283
+ from go_captcha_py import click, slide, rotate
284
+
285
+ # 自定义文字库 + 图片库(fonts / thumb_backgrounds 未传则用内置)
286
+ capt = click.make_text_captcha(
287
+ chars=["春", "夏", "秋", "冬", "风", "雪", "雷", "电"],
288
+ backgrounds=[Image.open("my-bg.jpg"), Image.open("my-bg2.jpg")],
289
+ )
290
+
291
+ # 自定义图形库(图形点击模式):dict[str, Image.Image]
292
+ # 图形数量同样需大于 range_len.max(默认 7)
293
+ capt = click.make_shape_captcha(
294
+ shapes={"star": Image.open("star.png"), "moon": Image.open("moon.png"),
295
+ "sun": Image.open("sun.png"), "cloud": Image.open("cloud.png"),
296
+ "fish": Image.open("fish.png"), "cat": Image.open("cat.png"),
297
+ "dog": Image.open("dog.png"), "bird": Image.open("bird.png")},
298
+ backgrounds=[Image.open("my-bg.jpg")],
299
+ )
300
+
301
+ # 自定义滑动拼图的背景与 tile(GraphImage: overlay/shadow/mask 三件套)
302
+ from go_captcha_py.slide import GraphImage
303
+ capt = slide.make_captcha(
304
+ backgrounds=[Image.open("my-bg.jpg")],
305
+ graph_images=[GraphImage(
306
+ overlay_image=Image.open("tile.png"),
307
+ shadow_image=Image.open("tile-shadow.png"),
308
+ mask_image=Image.open("tile-mask.png"),
309
+ )],
310
+ )
311
+
312
+ # 自定义旋转验证码背景
313
+ capt = rotate.make_captcha(images=[Image.open("square-photo.jpg")])
314
+ ```
315
+
316
+ > 注意:**文字库/图形库数量需大于 `range_len.max`(默认 7)**。素材较少时同步调小:
317
+ > `click.make_text_captcha(chars=[...], opts=[click.with_range_len(RangeVal(2, 2)), ...])`(shape 模式同理)
318
+
319
+ **方式二:Builder 链式(完全控制)**:
320
+
321
+ ```python
322
+ from PIL import Image
323
+ from go_captcha_py import click
324
+ from go_captcha_py.base.option import RangeVal, Size
325
+
326
+ capt = (
327
+ click.new_builder(
328
+ click.with_image_size(Size(400, 300)),
329
+ click.with_range_len(RangeVal(4, 6)),
330
+ )
331
+ .set_resources(
332
+ click.with_chars(["春", "夏", "秋", "冬", "风", "雪", "雷", "电"]),
333
+ click.with_backgrounds([Image.open("my-bg.jpg")]),
334
+ )
335
+ .make()
336
+ )
337
+ ```
338
+
339
+ **目录批量加载素材**(图片库较大时):
340
+
341
+ ```python
342
+ from pathlib import Path
343
+ from PIL import Image
344
+ from go_captcha_py import click
345
+
346
+ bgs = [Image.open(p).convert("RGB") for p in Path("./my-backgrounds").glob("*.jpg")]
347
+ capt = click.make_text_captcha(chars=list("我的自定义文字库种子字"), backgrounds=bgs)
348
+ ```
349
+
350
+ **内置文字库**(`go_captcha_py.resources`):
351
+
352
+ | 种子 | 获取方式 | 数量 |
353
+ | --- | --- | --- |
354
+ | 常用汉字 | `get_chinese_chars()` | 3498 |
355
+ | 纯字母 | `get_alpha_chars()` | 52 |
356
+ | 字母 + 数字 | `get_mixin_alpha_chars()` | 62 |
357
+
358
+ ## 开发
359
+
360
+ ```bash
361
+ ./dev.sh # 一键前后端联调(见上文)
362
+ uv sync --extra fastapi --dev # 安装全部依赖
363
+ uv run pytest tests/ -q # 测试(单元 + HTTP E2E)
364
+ uv run go-captcha-py ./out # 生成样例图到目录
365
+ uv run ruff check src tests examples # lint
366
+ uv run pre-commit run --all-files # 全量钩子(whitespace/yaml/toml/ruff…)
367
+ uv build # 构建 wheel + sdist
368
+ ```
369
+
370
+ 常用 make 目标:`make help` 查看全部(dev/test/lint/format/build/samples/e2e)。
371
+
372
+ 提交前钩子([.pre-commit-config.yaml](.pre-commit-config.yaml)):`uv run pre-commit install` 后每次 commit 自动跑 ruff / ruff-format / 文件检查,pre-push 阶段跑完整 pytest。
373
+
374
+ ## 项目结构
375
+
376
+ ```
377
+ src/go_captcha_py/
378
+ ├── base/ # option 类型、颜色/图像 helper、imagedata 封装、随机数
379
+ ├── click/ # 点击验证码(text/shape):生成、绘制、校验、选项
380
+ ├── slide/ # 滑动/拖拽拼图验证码
381
+ ├── rotate/ # 旋转验证码
382
+ ├── store/ # 答案缓存(MemoryStore + Store 协议)
383
+ ├── resources/ # 内置素材(图片/图形/tile/字体/3498字库,来自 go-captcha-resources)
384
+ └── cli.py # 命令行:批量生成样例
385
+ examples/
386
+ ├── fastapi_server.py # FastAPI 集成示例(6 个接口)
387
+ ├── vue-demo/ # Vue 2 + go-captcha-vue@^1 联调示例(含 Playwright E2E 脚本)
388
+ └── multi-framework/ # React / Svelte / Solid / JS / Angular 五个联调示例
389
+ tests/ # 单元测试 + FastAPI TestClient E2E 测试
390
+ ```
391
+
392
+ ## 参考 / 致谢
393
+
394
+ 本仓库是以下 Go 项目的 Python 复现,遵循其算法与交互协议,素材资源来自其资源仓库(Apache-2.0):
395
+
396
+ - **原项目 GoCaptcha(Go):** <https://github.com/wenlng/go-captcha>
397
+ - **素材资源仓库:** <https://github.com/wenlng/go-captcha-resources> / <https://github.com/wenlng/go-captcha-assets>
398
+ - **在线演示:** <https://gocaptcha.wencodes.com/>
399
+
400
+ 对接验证过的官方前端组件:
401
+
402
+ | 前端 | 仓库 |
403
+ | --- | --- |
404
+ | Vue | <https://github.com/wenlng/go-captcha-vue> |
405
+ | React | <https://github.com/wenlng/go-captcha-react> |
406
+ | Angular | <https://github.com/wenlng/go-captcha-angular> |
407
+ | Svelte | <https://github.com/wenlng/go-captcha-svelte> |
408
+ | Solid | <https://github.com/wenlng/go-captcha-solid> |
409
+ | JavaScript | <https://github.com/wenlng/go-captcha-jslib> |
410
+
411
+ 感谢 [@wenlng](https://github.com/wenlng) 的开源工作。
412
+
413
+ ## 发布到 PyPI
414
+
415
+ 版本号必须递增,已经发布过的版本不能覆盖。发布前建议运行:
416
+
417
+ ```bash
418
+ uv run pytest tests/ -q
419
+ uv run ruff check src tests examples
420
+ rm -rf dist
421
+ uv build --no-sources
422
+ ```
423
+
424
+ 配置 PyPI API Token 后发布:
425
+
426
+ ```bash
427
+ export UV_PUBLISH_TOKEN="pypi-..."
428
+ uv publish
429
+ ```
430
+
431
+ 也可以指定构建产物:
432
+
433
+ ```bash
434
+ uv publish dist/*
435
+ ```
436
+
437
+ 不要把 Token 写入仓库或提交到 `.env`。生产环境建议为本项目创建仅限项目范围的 PyPI Token。
438
+
439
+ ## License
440
+
441
+ [Apache-2.0](LICENSE)(与上游 go-captcha 一致)