标签:-- turn hello UNC int func web web编程 获取
一、框架分类
1、sockect+业务处理
该类框架有:tornado
2、只有业务逻辑,不包含socket,而是引用第三方组件(socket),这个第三方组件(socket)要遵循的协议就是:wsgi
框架有:django、bottle、flask 主要是逻辑处理、第三方的模块(包含了socket)
3、python中内置了wsgiref,它包含了socket
4、WEB框架原理
二、框架讲解
第一步:
from wsgiref.simple_server import make_server def new(): return ‘new‘ def index(): return ‘index‘ #用户请求所有内容封装在变量:environ #返回给用户所有信息变量:start_response def RunServer(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) #获取用户URL url = environ[‘PATH_INFO‘] if url == "/new": ret = new() elif url == "/index": ret = index() else: ret = "404" return ret if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8000, RunServer) print("Serving HTTP on port 8000...") httpd.serve_forever() 运行结果
改进版
from wsgiref.simple_server import make_server def new(): return ‘new‘ def index(): return ‘index‘ URLS = { "/new": new, "/index": index, } #用户请求所有内容封装在变量:environ #返回给用户所有信息变量:start_response def RunServer(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) #获取用户URL url = environ[‘PATH_INFO‘] # if url == "/new": # ret = new() # elif url == "/index": # ret = index() # else: # ret = "404" # return ret if url in URLS.keys(): func_name = URLS[url] ret = func_name() else: ret = "404" return ret if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8000, RunServer) print("Serving HTTP on port 8000...") httpd.serve_forever()
再进化
from wsgiref.simple_server import make_server def new(): return ‘<html><head></head><body><h1 style="color:red;">asdf</h1></body></html>‘ def index(): return ‘index‘ URLS = { "/new": new, "/index": index, } #用户请求所有内容封装在变量:environ #返回给用户所有信息变量:start_response def RunServer(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) #获取用户URL url = environ[‘PATH_INFO‘] # if url == "/new": # ret = new() # elif url == "/index": # ret = index() # else: # ret = "404" # return ret if url in URLS.keys(): func_name = URLS[url] ret = func_name() else: ret = "404" return ret if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8000, RunServer) print("Serving HTTP on port 8000...") httpd.serve_forever() 运行结果:
再向前进化一步
结构:
from wsgiref.simple_server import make_server def new(): f = open(‘new.html‘,‘r‘) data = f.read() return data def index(): return ‘index‘ URLS = { "/new": new, "/index": index, } #用户请求所有内容封装在变量:environ #返回给用户所有信息变量:start_response def RunServer(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) #获取用户URL url = environ[‘PATH_INFO‘] # if url == "/new": # ret = new() # elif url == "/index": # ret = index() # else: # ret = "404" # return ret if url in URLS.keys(): func_name = URLS[url] ret = func_name() else: ret = "404" return ret if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8000, RunServer) print("Serving HTTP on port 8000...") httpd.serve_forever()
运行结果:
再向前进化
V-- new.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>hello you</h1> </body> </html> C -- controller.py def new(): f = open(‘new.html‘,‘r‘) data = f.read() return data def index(): return ‘index‘ def home(): return ‘home‘ 路由:urls import controller URLS = { "/new": controller.new, "/index": controller.index, "/home": controller.home, } 启动程序:start.py from wsgiref.simple_server import make_server from urls import URLS #返回给用户所有信息变量:start_response def RunServer(environ, start_response): start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)]) #获取用户URL url = environ[‘PATH_INFO‘] if url in URLS.keys(): func_name = URLS[url] ret = func_name() else: ret = "404" return ret if __name__ == ‘__main__‘: httpd = make_server(‘‘, 8000, RunServer) httpd.serve_forever()
进一步升级:模板
看代码:
import os import time def new(): f = open(os.path.join(‘views‘,‘new.html‘),‘r‘) data = f.read() new_data = data.replace(‘{{itme}}‘,str(time.time())) return new_data def index(): f = open(os.path.join(‘views‘, ‘index.html‘), ‘r‘) data = f.read() return data def home(): f = open(os.path.join(‘views‘, ‘home.html‘), ‘r‘) data = f.read() return data
下面引入模块语言:第三方组件jingjia2 安装:pip3 install jingjia2
标签:-- turn hello UNC int func web web编程 获取
原文地址:https://www.cnblogs.com/xkjlive/p/9086249.html