from http.server import BaseHTTPRequestHandler, HTTPServer
from os import path
from urllib import request
from urllib.parse import urlparse
import json
import time
from PIL import Image
from io import BytesIO
import imageProcess
curdir = path.dirname(path.realpath(__file__))
sep = '/'
# MIME-TYPE
mimedic = [
('.html', 'text/html'),
('.htm', 'text/html'),
('.js', 'application/javascript'),
('.css', 'text/css'),
('.json', 'application/json'),
('.png', 'image/png'),
('.jpg', 'image/jpeg'),
('.gif', 'image/gif'),
('.txt', 'text/plain'),
('.avi', 'video/x-msvideo'),
]
class HTTPServer_RequestHandler(BaseHTTPRequestHandler):
global image_buf
# GET
def do_GET(self):
sendReply = False
querypath = urlparse(self.path)
filepath, query = querypath.path, querypath.query
if(querypath.path.startswith('/captcha')):
url = 'http://zhengzu.cangoonline.net/cas/imageAuthentication?timeTmp=%d' % time.time()
response = request.urlopen(url)
data = response.read()
self.send_response(200)
self.send_header('Content-Type', 'image/gif')
self.end_headers()
print('@@@@ new image fetched...')
self.wfile.write(data)
global image_buf
image_buf = BytesIO(data)
return
if filepath.endswith('/'):
filepath += 'index.html'
filename, fileext = path.splitext(filepath)
for e in mimedic:
if e[0] == fileext:
mimetype = e[1]
sendReply = True
print(path.realpath(curdir + sep +'public' + sep + filepath))
if sendReply == True:
try:
with open(path.realpath(curdir + sep +'public' + sep + filepath), 'rb') as f:
content = f.read()
self.send_response(200)
self.send_header('Content-type', mimetype)
self.end_headers()
self.wfile.write(content)
except IOError:
self.send_error(404, 'File Not Found: %s' % self.path)
def handleSubmit(self):
length = int(self.headers['Content-Length'])
post_data = self.rfile.read(length).decode('utf-8')
if len(post_data) > 0:
req = json.loads(post_data)
global image_buf
if (image_buf):
img = Image.open(image_buf)
lm, lv, om, ov, rm, rv = imageProcess.process(img, req['lNumber'], req['operator'], req['rNumber'])
response = [{lv: lm}, {ov: om}, {rv: rm}]
return response
def handleQuerySets(self):
import persistence
collection = persistence.openConnection()
keys = persistence.querySets(collection)
keys.sort()
result = {}
for key in keys:
result[key] = persistence.querySet(collection, key)
return result
def handlePredict(self):
global image_buf
if image_buf:
img = Image.open(image_buf)
v1, v2, v3 = imageProcess.predict(img)
print(v1, v2, v3)
response = [v1, v2, v3]
return response
# POST
def do_POST(self):
querypath = urlparse(self.path)
response = []
if querypath.path == '/submit':
response = self.handleSubmit()
if querypath.path == '/query':
response = self.handleQuerySets()
if querypath.path == '/predict':
response = self.handlePredict()
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(response).encode('UTF-8'))
def run():
port = 8000
print('starting server, port', port)
# Server settings
server_address = ('', port)
httpd = HTTPServer(server_address, HTTPServer_RequestHandler)
print('running server...')
httpd.serve_forever()
if __name__ == '__main__':
run()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1、该资源内项目代码经过严格调试,下载即用确保可以运行! 2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、电子信息等)正在做课程设计、期末大作业和毕设项目的学生、或者相关技术学习者作为学习资料参考使用。 3、该资源包括全部源码,需要具备一定基础才能看懂并调试代码。 基于机器的图形验证码识别入门学习资料源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于机器的图形验证码识别入门学习资料源码.zip (13个子文件)
project_code_0628
imageProcess.py 3KB
.idea
other.xml 233B
vcs.xml 180B
misc.xml 185B
modules.xml 276B
CangoCaptcha.iml 554B
public
app.js 4KB
index.html 2KB
index.css 533B
trainingSrv.py 4KB
.gitignore 1KB
train.py 1KB
persistence.py 1KB
共 13 条
- 1
资源评论
辣椒种子
- 粉丝: 3405
- 资源: 5725
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功