基于python實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)服務(wù)器代碼實(shí)例
代碼:
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。它實(shí)現(xiàn)了wsgi接口,我們只需要定一個(gè)wsgi處理函數(shù)來(lái)處理得到的請(qǐng)求就可以了。
用python來(lái)實(shí)現(xiàn)這些看似很復(fù)雜的實(shí)例程序,非常簡(jiǎn)單,這都得益于python強(qiáng)大的庫(kù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA刪除類的方法步驟2. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法3. idea自定義快捷鍵的方法步驟4. docker /var/lib/docker/aufs/mnt 目錄清理方法5. 詳解PHP laravel中的加密與解密函數(shù)6. 刪除docker里建立容器的操作方法7. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法8. IntelliJ IDEA導(dǎo)入項(xiàng)目的方法9. Intellij IDEA 關(guān)閉和開(kāi)啟自動(dòng)更新的提示?10. IntelliJ IDEA配置Tomcat服務(wù)器的方法

網(wǎng)公網(wǎng)安備