
#!/usr/bin/python3from wsgiref.simple_server import make_serverfrom wsgiref.util import setup_testing_defaultsimport webbrowser # open user's web browser to url when server is runfrom sys import exc_infofrom traceback import format_tb# Easily serves an HTML form at path_to_index with style at path_to_style# Calls on_submit when the form is submitted,passing a dictionary with key# value pairs { "input name" : submitted_value }class SimpleServer: def __init__(self,port=8000,on_submit=None,index_path="./index.HTML",CSS_path="./style.CSS"): self.port = port self.on_submit = on_submit self.index_path = index_path self.CSS_path = CSS_path # Forwards request to proper method,or returns 404 page def wsgi_app(self,environ,start_response): urls = [ (r"^$",self.index),(r"404$",self.error_404),(r"style.CSS$",self.CSS) ] path = environ.get("PATH_INFO","").lstrip("/") # Call another application if they called a path defined in urls for regex,application in urls: match = re.search(regex,path) # if the match was found,return that page if match: environ["myapp.url_args"] = match.groups() return application(environ,start_response) return error_404(environ,start_response) # Gives the user a form to submit all their input. If the form has been # submitted,it sends the ouput of self.on_submit(user_input) def index(self,start_response): # user_input is a dictionary,with keys from the names of the fIElds user_input = parse_qs(environ['query_STRING']) # return either the form or the calculations index_HTML = open(self.index_path).read() body = index_HTML if user_input == {} else calculate(user_input) mime_type = "text/HTML" if user_input == {} else "text/plain" # return the body of the message status = "200 OK" headers = [ ("Content-Type",mime_type),("Content-Length",str(len(body))) ] start_response(status,headers) return [body.encode("utf-8")] def start_form(self): httpd = make_server('',self.port,ExceptionMIDdleware(self.wsgi_app)) url = "http://localhost:" + str(self.port) print("Visit " + url) # webbrowser.open(url) httpd.serve_forever()if __name__ == "__main__": server = SimpleServer() server.start_form() 当我运行它时,我得到了错误
127.0.0.1 - - [16/Dec/2014 21:15:57] "GET / http/1.1" 500 0Traceback (most recent call last): @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 138,in run self.finish_response() @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 180,in finish_response self.write(data) @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 266,in write "write() argument must be a bytes instance"AssertionError: write() argument must be a bytes instance127.0.0.1 - - [16/Dec/2014 21:15:57] "GET / http/1.1" 500 59----------------------------------------Exception happened during processing of request from ('127.0.0.1',49354)Traceback (most recent call last): @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",in write "write() argument must be a bytes instance"AssertionError: write() argument must be a bytes instanceDuring handling of the above exception,another exception occurred:Traceback (most recent call last): @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 141,in run self.handle_error() @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 368,in handle_error self.finish_response() @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 274,in write self.send_headers() @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 331,in send_headers if not self.origin_server or self.clIEnt_is_modern(): @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 344,in clIEnt_is_modern return self.environ['SERVER_PROTOCol'].upper() != 'http/0.9'TypeError: 'nonetype' object is not subscriptableDuring handling of the above exception,another exception occurred:Traceback (most recent call last): @R_301_6852@ "/usr/lib/python3.4/socketserver.py",line 305,in _handle_request_noblock self.process_request(request,clIEnt_address) @R_301_6852@ "/usr/lib/python3.4/socketserver.py",in process_request self.finish_request(request,in finish_request self.RequestHandlerClass(request,clIEnt_address,self) @R_301_6852@ "/usr/lib/python3.4/socketserver.py",line 669,in __init__ self.handle() @R_301_6852@ "/usr/lib/python3.4/wsgiref/simple_server.py",line 133,in handle handler.run(self.server.get_app()) @R_301_6852@ "/usr/lib/python3.4/wsgiref/handlers.py",line 144,in run self.close() @R_301_6852@ "/usr/lib/python3.4/wsgiref/simple_server.py",line 35,in close self.status.split(' ',1)[0],self.bytes_sentAttributeError: 'nonetype' object has no attribute 'split' 此输出实际上不包括我正在运行的脚本,我很困惑.有什么想法吗?
解决方法 只是为这个问题注册解决方案,问题在于len()函数.STR(LEN(本体))
它计算错误的大小,当返回服务器Content-Length时,它等待所需的更多字节.
因此,总是使用UTF-8缓冲区发送字节,例如:
from io import StringIOstdout = StringIO()print("Hello World!",@R_301_6852@=stdout)start_response("200 OK",[('Content-Type','text/plain; charset=utf-8')])return [stdout.getvalue().encode("utf-8")] 总结 以上是内存溢出为你收集整理的使用WSGI提供html时出错全部内容,希望文章能够帮你解决使用WSGI提供html时出错所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)