基于WSGI封装一个简单WEB框架

Posted FreeMan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于WSGI封装一个简单WEB框架相关的知识,希望对你有一定的参考价值。

from wsgiref.simple_server import make_server
def index():
return ‘index‘
def login():
return ‘login‘
def routes():
urlpatterns=(
(‘/index‘,index),
(‘/login‘,login),
)
return urlpatterns
def runserver(environ,start_response):
start_response(‘200 OK‘ , [(‘Content-Type‘,‘text/html‘)])
url = environ[‘PATH_INFO‘]
urlpatterns = routes()
func = None
for item in urlpatterns:
print (item)
if item[0] == url:
func=item[1]
break
if func:
a=func()
return a
else:
return ‘hhhhh404 404‘
if __name__ == ‘__main__‘:
httpd= make_server(‘192.168.6.163‘,8000,runserver)
print (‘start web server‘)
httpd.serve_forever()

以上是关于基于WSGI封装一个简单WEB框架的主要内容,如果未能解决你的问题,请参考以下文章

python框架Django与WSGI

使用WSGI创建REST接口

Python Web 应用:WSGI基础

Python Web 应用:WSGI基础

django框架--底层架构

Python Web框架简介