首页 > 编程语言 > 详细

python简单网页服务器示例

时间:2014-11-02 22:22:18      阅读:359      评论:0      收藏:0      [点我收藏+]

参考:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832689740b04430a98f614b6da89da2157ea3efe2000

代码:

hello.py

1 #!/usr/bin/python
2 # coding: utf-8
3 
4 # hello.py
5 def application(environ, start_response):
6     start_response(200 OK, [(Content-Type, text/html)])
7     return <h1>Hello, %s!</h1> % (environ[PATH_INFO][1:] or web)

server.py

 1 #!/usr/bin/python
 2 # coding: utf-8
 3 
 4 # server.py
 5 from wsgiref.simple_server import make_server
 6 from hello import application
 7 
 8 # create server, ip is empty, port is 8000, handle function is application
 9 httpd = make_server(‘‘, 8000, application)
10 print "Serving HTTP on port 8000..."
11 # start listen http request
12 httpd.serve_forever()

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

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

 

python简单网页服务器示例

原文:http://www.cnblogs.com/lit10050528/p/4070052.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!