基于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強大的庫。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. python如何換行輸出2. Python使用urlretrieve實現直接遠程下載圖片的示例代碼3. Python:UserWarning:此模式具有匹配組。要實際獲得組,請使用str.extract4. Android Studio中一套代碼多渠道打包的實現方法5. 詳解java google Thumbnails 圖片處理6. python如何計算圓的面積7. Java使用Tesseract-Ocr識別數字8. Android打包篇:Android Studio將代碼打包成jar包教程9. Java 接口和抽象類的區別詳解10. 解決Android Studio 格式化 Format代碼快捷鍵問題
