基于python实现简单网页服务器代码实例

Python itxz 5年前 (2020-11-03) 262次浏览 已收录 0个评论

代码

hello.py


#!/usr/bin/<a href="http://www.itxz.com/?tag=python" title="查看更多关于python的文章" target="_blank">python</a>
# coding: utf-8
# hello.py
def application(environ, start_response):
  start_response('200 OK', [('Content-Type', 'text/html')])
  return '<h1>Hello, %s!</h1>' % (environ['PATH_INFO'][1:] or 'web')

server.py

#!/usr/bin/<a href="http://www.itxz.com/?tag=python" title="查看更多关于python的文章" target="_blank">python</a>
# coding: utf-8
 
# server.py
from wsgiref.simple_server import make_server
from hello import application
 
# create server, ip is empty, port is 8000, handle function is application
httpd = make_server('', 8000, application)
print "Serving HTTP on port 8000..."
# start listen http request
httpd.serve_forever()

使用了模块wsgiref。它实现了wsgi接口,我们只需要定一个wsgi处理函数来处理得到的请求就可以了。

python来实现这些看似很复杂的实例程序,非常简单,这都得益于python强大的库。


IT学者 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:基于python实现简单网页服务器代码实例
喜欢 (0)

您必须 登录 才能发表评论!