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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import os #Python的标准库中的os模块包含普遍的操作系统功能 import re #引入正则表达式对象 import urllib #用于对URL进行编解码 if sys.version_info < (3, 0): from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler #导入HTTP处理相关的模块 else : from http.server import HTTPServer, BaseHTTPRequestHandler import cgi #文件上传处理 import base64 #base64编码 from io import BytesIO #本地文件读取 #自定义处理程序,用于处理HTTP请求 class EasyHTTPHandler(BaseHTTPRequestHandler): def do_RespHtml(self, html): self.protocal_version = 'HTTP/1.1' #设置协议版本 self.send_response(200) #设置响应状态码 self.send_header("Welcome", "Contect") #设置响应头 self.end_headers() if sys.version_info >= (3, 0): html = html.encode('utf-8') self.wfile.write(html) #输出响应内容 def do_ImageHtml(self, img): templateStr = ''' <html> <head> <title>EasyHttpServer</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form enctype="multipart/form-data" method="post"> 选择图片:<input type="file" name="uploadImage" id="uploadImage" accept="image/png, image/jpeg"> <input type="submit" value="识别"> </form> <div>{0}</div> </body> </html> '''.format(img) self.do_RespHtml(templateStr) def do_FileRecv(self): form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], } ) imageHtml = "" for field in form.keys(): field_item = form[field] filename = field_item.filename filevalue = field_item.value filesize = len(filevalue)#文件大小(字节) #print len(filevalue) #print (filename) ext = os.path.splitext(filename)[1] if sys.version_info >= (3, 0): filename = filename.encode('utf-8') with open(filename, 'wb') as f: f.write(filevalue) #将图片保存到内存中 f = BytesIO() with open(filename, 'rb') as img: f.write(img.read()) #从内存中取出bytes类型的图片 data = f.getvalue() #将bytes转成base64 data = base64.b64encode(data).decode() imageHtml += "<p> <image width='200px' src='data:image/" + ext + ";base64," + data + "'/> </p>" self.do_ImageHtml(imageHtml) def doIndexHtml(self): #获取URL #print 'URL=',self.path #页面输出模板字符串 templateStr = ''' <html> <head> <title>EasyHttpServer</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form enctype="multipart/form-data" method="post"> 选择图片:<input type="file" name="uploadImage" id="uploadImage" accept="image/png, image/jpeg"> <input type="submit" value="上传"> </form> </body> </html> ''' self.do_RespHtml(templateStr) #处理GET请求 def do_GET(self): self.doIndexHtml() #处理POST请求 def do_POST(self): self.do_FileRecv() #启动服务函数 def start_server(port): http_server = HTTPServer(('', int(port)), EasyHTTPHandler) http_server.serve_forever() #设置一直监听并接收请求 #os.chdir('static') #改变工作目录到 static 目录 start_server(8000) #启动服务,监听8000端口 |
每日归档: 2019年11月23日
Visual Studio Code调试时报错"raise RuntimeError('already started')"
Visual Studio Code远程调试pytorch模型训练时,报错如下:
1 2 3 |
File "/home/xxxx/.vscode-server/extensions/ms-python.python-2019.5.18875/pythonFiles/lib/python/ptvsd/daemon.py", line 145, in start raise RuntimeError(‘already started’) RuntimeError: already started |
解决办法,在.py文件头添加如下语句:
1 2 |
import multiprocessing multiprocessing.set_start_method('spawn',True) |
更新:
如果你的工程是基于pytorch的,那么检查自己的dataLoader,是否使用了num_workers参数。当使用该参数时,可能会报上述错误。解决办法是将num_workers设置为0