基于python实现简单网页服务器代码实例
代码:
hello.py
#!/usr/bin/python # 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/python # 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强大的库。
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16