标签:sim pat pen style log 参数 web app int 获取
WSGI(Web Server Gateway Interface)是一种规范,它定义了使用python编写的web app与web server之间接口格式,实现web app与web server间的解耦。
python标准库提供的独立WSGI服务器称为wsgiref。
1 from wsgiref.simple_server import make_server 2 3 4 def RunServer(environ, start_response): 5 start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) 6 return [bytes(‘<h1>Hello, web!</h1>‘, encoding=‘utf-8‘), ] 7 8 9 if __name__ == ‘__main__‘: 10 httpd = make_server(‘‘, 8000, RunServer) 11 print("Serving HTTP on port 8000...") 12 httpd.serve_forever()
详解environ参数的由来
简单的从用户输入中获取url,并处理请求
1 #!usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 # wsgiref遵循了WSGI规范,内部帮我们实现了socket, 5 from wsgiref.simple_server import make_server 6 7 def new(): 8 f=open("reg.html","r") 9 data=f.read() 10 f.close() 11 return data 12 def index(): 13 return "index" 14 URLS={ 15 "/new":new, 16 "/index":index, 17 } 18 def RunServer(environ, start_response): 19 start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) 20 url=environ[‘PATH_INFO‘] 21 if url=="/index": 22 ret=index() 23 elif:url=="/new" 24 ret=new() 25 else: 26 ret="404" 27 return ret 28 if __name__ == ‘__main__‘: 29 httpd = make_server(‘‘, 8000, RunServer) 30 httpd.serve_forever()
这样写业务逻辑非常的不好,一旦url很多,就非常的麻烦,改进如下:
1 #!usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 # wsgiref遵循了WSGI规范,内部帮我们实现了socket, 5 from wsgiref.simple_server import make_server 6 7 def new(): 8 f=open("reg.html","r") 9 data=f.read() 10 f.close() 11 return data 12 def index(): 13 return "index" 14 URLS={ 15 "/new":new, 16 "/index":index, 17 } 18 def RunServer(environ, start_response): 19 start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) 20 url=environ[‘PATH_INFO‘] 21 if url in URLS.keys(): 22 func_name=URLS[url] 23 ret=func_name() 24 else: 25 ret="404" 26 return ret 27 if __name__ == ‘__main__‘: 28 httpd = make_server(‘‘, 8000, RunServer) 29 httpd.serve_forever()
为了进入步管理web框架的各个文件,对文件进行了处理
其实 MVC 和MTV思想都是对各个文件进行分类处理,并执行各个的任务
MVC
Models : 数据相关操作
Views : 模版HTML文件
Controllers 业务逻辑
MTV
Models : 数据相关操作
Temp;ates : 模版HTML文件
Views业务逻辑
标签:sim pat pen style log 参数 web app int 获取
原文地址:http://www.cnblogs.com/jianquan/p/6940024.html