为了演示基本的HTTP处理,我正在尝试定义一个非常小的HTTP服务器演示的过程。我一直在使用优秀的werkzeug库,我试图“多一些”。我目前的服务器确实太多:)最小的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内部服务器错误作为一切。理想的情况是从服务器的响应应该是500任何HTTP请求,只是说,因为服务器不知道请求任何
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
我想删除至少在服务器,哪些是可选的日期。
所以,你要HTTP的实现,其通过回复'500内部服务器ERROR'一切符合? – Potatoswatter
是的。几分钟后,我取得了一些进展。 – karlcow
是否有必要使用库?只要打开一个TCP端口并在任何人发送两条换行符时打印字符串。 – Potatoswatter