Python-WSGI的简单尝试(Mac+Python3.7+VSCode)
程序员文章站
2022-06-06 19:01:57
...
环境: Mac + Python3.7 + VSCode
WSGI其实是一个借口,定义了服务器和客户端之间的标准通信格式,这样可以只要关注自己的html代码即可。
from wsgiref.simple_server import make_server
def application(environ, start_response):
status = '200 OK'
response_headers = [('Content-Type', 'text/html')]
start_response(status, response_headers)
return ['<h1> Hello, web!</h1>'.encode('utf-8')]
httpd = make_server('localhost', 8051, application)
print('Serving HTTP on port 8051...')
httpd.serve_forever()
直接在终端运行 xxx.py 就可以了
然后,在浏览器上输入 http://localhost:8051/ 就能看到 Hello, web! 了。
另外,要注意,如果在return那里没有加转码,在3.7下面就会报下面的错:
127.0.0.1 - - [02/Sep/2019 09:53:20] "GET / HTTP/1.1" 200 0
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [02/Sep/2019 09:53:20] "GET / HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 51737)
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 141, in run
self.handle_error()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 368, in handle_error
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 344, in process_request
self.finish_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/anaconda3/lib/python3.7/socketserver.py", line 717, in __init__
self.handle()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 133, in handle
handler.run(self.server.get_app())
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 144, in run
self.close()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
127.0.0.1 - - [02/Sep/2019 09:53:20] "GET / HTTP/1.1" 200 0
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [02/Sep/2019 09:53:20] "GET / HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 51739)
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 141, in run
self.handle_error()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 368, in handle_error
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 344, in process_request
self.finish_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/anaconda3/lib/python3.7/socketserver.py", line 717, in __init__
self.handle()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 133, in handle
handler.run(self.server.get_app())
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 144, in run
self.close()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
127.0.0.1 - - [02/Sep/2019 09:53:20] "GET / HTTP/1.1" 200 0
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [02/Sep/2019 09:53:20] "GET / HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 51741)
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 141, in run
self.handle_error()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 368, in handle_error
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 344, in process_request
self.finish_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/anaconda3/lib/python3.7/socketserver.py", line 717, in __init__
self.handle()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 133, in handle
handler.run(self.server.get_app())
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 144, in run
self.close()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
127.0.0.1 - - [02/Sep/2019 09:53:20] "GET / HTTP/1.1" 200 0
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [02/Sep/2019 09:53:20] "GET / HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 51743)
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 141, in run
self.handle_error()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 368, in handle_error
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 344, in process_request
self.finish_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/anaconda3/lib/python3.7/socketserver.py", line 717, in __init__
self.handle()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 133, in handle
handler.run(self.server.get_app())
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 144, in run
self.close()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
127.0.0.1 - - [02/Sep/2019 09:53:25] "GET / HTTP/1.1" 200 0
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [02/Sep/2019 09:53:25] "GET / HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 51748)
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 141, in run
self.handle_error()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 368, in handle_error
self.finish_response()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/lib/python3.7/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 344, in process_request
self.finish_request(request, client_address)
File "/anaconda3/lib/python3.7/socketserver.py", line 357, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/anaconda3/lib/python3.7/socketserver.py", line 717, in __init__
self.handle()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 133, in handle
handler.run(self.server.get_app())
File "/anaconda3/lib/python3.7/wsgiref/handlers.py", line 144, in run
self.close()
File "/anaconda3/lib/python3.7/wsgiref/simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
虽然一大堆,但是我们看最前面提示的 “AssertionError: write() argument must be a bytes instance”,意思就是需要 bytes 类型的,而我们直接返回的字符串,所以才会出错。
下一篇: 浅析绿萝算法下个人站长究竟该如何建设外链