-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcode_server.py
More file actions
92 lines (79 loc) · 2.19 KB
/
code_server.py
File metadata and controls
92 lines (79 loc) · 2.19 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#coding:utf8
'''
验证码图片类型说明
1:http://shixin.court.gov.cn/image.jsp
2:http://zhixing.court.gov.cn/search/security/jcaptcha.jpg
'''
import os
import md5
import time
import random
import traceback
from collections import defaultdict
from bottle import run, post, request
from PIL import Image
from main.util import buildvector
code_template_dic = {
1:{},
2:{}
}
curpath = os.path.dirname(__file__)
def load_template(path):
template_dic = defaultdict(list)
for num in range(10):
dir_path = os.path.join(path, '%s'%num)
files = os.listdir(dir_path)
images = [Image.open(os.path.join(dir_path, filename)) for filename in files]
template_dic[num] = [buildvector(img) for img in images]
for img in images:
if hasattr(img, 'close'):
img.close()
return template_dic
def load_all():
global code_template_dic
for code_typ in code_template_dic.keys():
path = os.path.join(curpath, 'main', 'template', '%s'%code_typ)
template_dic = load_template(path)
code_template_dic.update({
code_typ:template_dic
})
return
#加载模版
load_all()
@post('/code')
def code_verify_out():
try:
code = code_verify(request)
except Exception as e:
print traceback.format_exc()
code = 'ERROR'
return code
def code_verify(request):
image_str = request.forms.get("code")
typ = int(request.forms.get('type'))
filename = os.path.join(
curpath,
'temp',
'%s.png'%(
md5.md5(
str(time.time()) + str(random.random())
).hexdigest()
)
)
with open(filename, 'wb') as f:
f.write(image_str)
img = Image.open(filename)
exec '''from main import main_%s'''%(typ)
exec '''code = main_%s.verify(img, code_template_dic[typ])'''%(typ)
#exec '''code = main_%s.verify(img)'''%(typ)
if hasattr(img, 'close'):
img.close()
try:
print os.remove(filename)
except Exception as e:
print u"删除图片失败 %s"%e
return code
run(host="0.0.0.0", port=8080)
if __name__ == "__main__":
#load_all()
pass