为了演示基本的 HTTP 处理,我正在尝试定义一个真正最小的 HTTP 服务器演示。我一直在使用出色的werkzeug库,我正试图将其“哑巴”一点。我当前的服务器做的太多了:)
#!/usr/bin/env python2.7
# encoding: utf-8
if __name__ == '__main__':
from werkzeug.serving import run_simple
run_simple('127.0.0.1', 6969, application=None)
run_simple
处理的事情已经太多了。向该服务器发出请求时,
→ http GET http://127.0.0.1:6969/
我们得到:
HTTP/1.0 500 INTERNAL SERVER ERROR
Content-Type: text/html
Content-Length: 291
Server: Werkzeug/0.8.3 Python/2.7.1
Date: Tue, 08 Jan 2013 07:45:46 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was
unable to complete your request. Either the server
is overloaded or there is an error in the application.</p>
我很想把它减少到最低限度。并使用 500 Internal Server Error 作为包罗万象的方法。理想情况下,对于任何 HTTP 请求,来自服务器的响应应该是 500,仅此而已,因为服务器对请求一无所知
HTTP/1.0 500 INTERNAL SERVER ERROR
然后在第二阶段,我可能会添加
HTTP/1.0 500 INTERNAL SERVER ERROR
Content-Type: text/plain
Internal Server Error
然后通过理解请求开始处理请求。目标是在此过程中具有教育意义。欢迎任何关于接管默认答案的建议。
更新 001
和:
#!/usr/bin/env python2.7
# encoding: utf-8
from werkzeug.wrappers import BaseResponse as Response
def application(environ, start_response):
response = Response('Internal Server Error', status=500)
return response(environ, start_response)
if __name__ == '__main__':
from werkzeug.serving import run_simple
run_simple('127.0.0.1', 6969, application)
它会回来
HTTP/1.0 500 INTERNAL SERVER ERROR
Content-Type: text/plain; charset=utf-8
Content-Length: 21
Server: Werkzeug/0.8.3 Python/2.7.1
Date: Tue, 08 Jan 2013 07:55:10 GMT
Internal Server Error
我想至少删除可选的服务器和日期。