新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python中實(shí)現(xiàn)WSGI的框架
1、說明

創(chuàng)新互聯(lián)成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元五華做網(wǎng)站,已為上家服務(wù),為五華各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
Application類對WSGI又做了一層簡單的封裝,由于上面說過WSGI函數(shù)返回的是一個(gè)可以迭代對象,所以需要實(shí)現(xiàn)一個(gè)__iter__方法,里面控制了客戶端的請求路由并且返回不同的輸出。
2、實(shí)例
from wsgiref.simple_server import make_server
class Application(object):
def __init__(self, environ, start_response):
self.start_response = start_response
self.path = environ['PATH_INFO']
def __iter__(self):
if self.path == '/':
status = '200 OK'
response_headers = [('Content-type', 'text/html')]
self.start_response(status, esponse_headers)
yield 'Hello,World!
'.encode('utf-8')
elif self.path == '/wsgi':
status = '200 OK'
response_headers = [('Content-type', 'text/html')]
self.start_response(status, response_headers)
yield 'Hello,WSGI!
'.encode('utf-8')
else:
status = '404 NOT FOUND'
response_headers = [('Content-type', 'text/html')]
self.start_response(status, response_headers)
yield '404 NOT FOUND
'.encode('utf-8')
if __name__ == "__main__":
app = make_server('127.0.0.1', 8000, Application)
print('Serving HTTP on port 8000...')
app.serve_forever()以上就是python中實(shí)現(xiàn)WSGI的框架,希望對大家有所幫助。更多Python學(xué)習(xí)推薦:python教學(xué)
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:Python中實(shí)現(xiàn)WSGI的框架
瀏覽路徑:http://fisionsoft.com.cn/article/coideoh.html


咨詢
建站咨詢
