-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdebug_flask_server.py
More file actions
executable file
·65 lines (48 loc) · 1.65 KB
/
debug_flask_server.py
File metadata and controls
executable file
·65 lines (48 loc) · 1.65 KB
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
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
# 公
import sys
import os.path as op
import multiprocessing as mp
from flask import Flask, request, render_template_string
from flask_simple_captcha import CAPTCHA, DEFAULT_CONFIG
app = Flask(__name__)
test_config = DEFAULT_CONFIG.copy()
CAPTCHA = CAPTCHA(config=test_config)
app = CAPTCHA.init_app(app)
PROCS = mp.cpu_count()
def _captcha(mp_list: list):
mp_list.append(CAPTCHA.create())
@app.route('/', methods=['GET', 'POST'])
def submit_captcha():
if request.method == 'GET':
captcha_dict = CAPTCHA.create()
capinput = CAPTCHA.captcha_html(captcha_dict)
return render_template_string(
'<form method="POST">%s<input type="submit"></form>' % capinput
)
if request.method == 'POST':
c_hash = request.form.get('captcha-hash')
c_text = request.form.get('captcha-text')
if CAPTCHA.verify(c_text, c_hash):
return 'success'
else:
return 'failed captcha'
@app.route('/images')
@app.route('/images/<int:captchas>')
def bulk_captchas(captchas=None):
captchas = captchas or 50
with mp.Manager() as mgr:
mp_list = mgr.list()
with mp.Pool(PROCS) as pool:
pool.map(_captcha, [mp_list] * captchas)
captchas = list(mp_list)
mimetype = 'image/png' if CAPTCHA.img_format == 'PNG' else 'image/jpeg'
captchas = [
'<img class="simple-captcha-img" '
+ 'src="data:%s;base64, %s" />' % (mimetype, c['img'])
for c in captchas
]
style = '<style>img display: inline-block; margin: 8px;</style>'
return style + '\n'.join(captchas)
if __name__ == '__main__':
app.run()