基于python實現簡單網頁服務器代碼實例
代碼:
hello.py
#!/usr/bin/python# coding: utf-8# hello.pydef 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.pyfrom wsgiref.simple_server import make_serverfrom hello import application# create server, ip is empty, port is 8000, handle function is applicationhttpd = make_server(’’, 8000, application)print 'Serving HTTP on port 8000...'# start listen http requesthttpd.serve_forever()
使用了模塊wsgiref。它實現了wsgi接口,我們只需要定一個wsgi處理函數來處理得到的請求就可以了。
用python來實現這些看似很復雜的實例程序,非常簡單,這都得益于python強大的庫。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: