HTTP是文本协议

可以在chrome查看具体请求和返回数据:F12-Netwoks-红色recording network-点击具体链接或刷新-点相关资源-Headers

Python版简单的HTTP服务器

请求:request

GET / HTTP/1.1\r\n

HOST:www.xx.com.cn\r\n

……

Body data here

返回:response

HTTP/1.1 200 OK

Content-type:text/html

……

Body

WSGI

WSGI是web server gateway interface实现一个Http处理函数就可以响应http请求。

def application(environ, start_response):

    start_response('200 OK', [('Content-Type', 'text/html')])

return '<h1>Hello, web!</h1>'

其中environ:一个包含所有HTTP请求信息的dict对象;start_response:一个发送HTTP响应的函数。

application()函数必须由WSGI服务器来调用,Python内置了一个WSGI服务器,这个模块叫wsgiref。

编写一个server.py,负责启动WSGI服务器,加载application()函数:

from wsgiref.simple_server import make_server

httpd = make_server('', 8000, application)

print "Serving HTTP on port 8000..."

httpd.serve_forever()

运行即可

 

相关文章:

  • 2021-06-20
  • 2022-12-23
  • 2021-09-15
  • 2021-08-16
  • 2021-06-03
  • 2022-12-23
  • 2021-10-01
猜你喜欢
  • 2022-12-23
  • 2022-01-01
  • 2021-06-13
  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案