Skip to content

Commit

Permalink
Update server.py
Browse files Browse the repository at this point in the history
smxiazi authored May 6, 2021
1 parent 913e774 commit 7fcaa5d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import re,time,base64,os

host = ('0.0.0.0', 8899)
count = 50 #保存多少个验证码及结果

class Resquest(BaseHTTPRequestHandler):
def handler(self):
@@ -16,8 +17,9 @@ def do_GET(self):
if self.path != '/':
self.send_error(404, "Page not Found!")
return

data = '<title>xp_CAPTCHA</title><body style="text-align:center"><h1>验证码识别:xp_CAPTCHA</h1><a href="http://www.nmd5.com">author:算命縖子</a></body>'
with open('temp/log.txt', 'r') as f:
content = f.read()
data = '<title>xp_CAPTCHA</title><body style="text-align:center"><h1>验证码识别:xp_CAPTCHA</h1><a href="http://www.nmd5.com">author:算命縖子</a><p><TABLE style="BORDER-RIGHT: #ff6600 2px dotted; BORDER-TOP: #ff6600 2px dotted; BORDER-LEFT: #ff6600 2px dotted; BORDER-BOTTOM: #ff6600 2px dotted; BORDER-COLLAPSE: collapse" borderColor=#ff6600 height=40 cellPadding=1 align=center border=2><tr align=center><td>验证码</td><td>识别结果</td><td>时间</td></tr>%s</body>'%(content)
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=UTF-8')
self.end_headers()
@@ -47,6 +49,21 @@ def do_POST(self):
text = sdk.predict(image_bytes=b)
print(text) #识别的结果

#保存最新count个的验证码及识别结果
with open('temp/log.txt', 'r') as f:
data = ""
counts = 0
content = f.read()
pattern = re.compile(r'.*?\n')
result1 = pattern.findall(content)
for i in result1:
counts += 1
if counts >= count: break
data = data + i

with open('temp/log.txt', 'w') as f:
f.write('<tr align=center><td><img src="data:image/png;base64,%s"/></td><td>%s</td><td>%s</td></tr>\n'%(base64_img.group(1),text,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(img_name))))+ data)

#删除掉图片文件,以防占用太大的内存
os.remove("temp/%s.png"%img_name)

@@ -57,6 +74,8 @@ def do_POST(self):

if __name__ == '__main__':
os.makedirs('temp', exist_ok=True)
with open('temp/log.txt', 'w') as f:
pass
server = HTTPServer(host, Resquest)
print("Starting server, listen at: %s:%s" % host)
server.serve_forever()
server.serve_forever()

0 comments on commit 7fcaa5d

Please sign in to comment.