基于python實現(xiàn)簡單網(wǎng)頁服務(wù)器代碼實例
代碼:
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。它實現(xiàn)了wsgi接口,我們只需要定一個wsgi處理函數(shù)來處理得到的請求就可以了。
用python來實現(xiàn)這些看似很復(fù)雜的實例程序,非常簡單,這都得益于python強(qiáng)大的庫。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進(jìn))2. .Net反向代理組件Yarp用法詳解3. 解決request.getParameter取值后的if判斷為NULL的問題4. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別5. 詳解JSP 內(nèi)置對象request常見用法6. JSP中param動作的實例詳解7. ASP.NET MVC實現(xiàn)下拉框多選8. ASP.NET MVC增加一條記錄同時添加N條集合屬性所對應(yīng)的個體9. .NET中的MassTransit分布式應(yīng)用框架詳解10. ASP.NET MVC實現(xiàn)本地化和全球化
