01 Web框架介绍
2024-08-29 23:18:18
一、Web框架本质
- 所有的web应用程序本质上都是socket,用户的浏览器其实就是一个socket客户端。
- python中常用的web框架有:
- Django
- Flask
- web.py
- WSGI(web server gateway interface)定义了使用python编程的web app和web server之间的接口格式,实现了服务端与客户端的解耦。
- pytho标准库提供的独立WSGI服务器称为wsgired。
二、利用wsgrired自定义Web框架
#!/usr/local/bin/python3
#-*-coding:utf-8 -*-
#Author:Felix Song ;Environment:pycharm 5.0.3(python3.6) #用python标准库开发一个自己的web框架
from wsgiref.simple_server import make_server #第二步改造,获取conf中的内容
import conf_url def RunServer(environ,start_response):
start_response('200 OK',[('Content-Type','text/html')])
#第一步,获取用户URL,debug模式下,在start_response加断点,然后浏览器访问在Variables下的environ中找
userUrl = environ['PATH_INFO']
print(userUrl)
urlpatterns = conf_url.routes() #第三步改造
#第二步改造
func = None
# for item in conf_url.url: #第三步注释掉
for item in urlpatterns:#第三步改造
if item[0] == userUrl:
func = item[1]
break if func:
return func()
else:
return [bytes('<h1>404</h1>',encoding='utf-8')] '''
#第二步,根据URL输入的不同返回不同的值,但是如果页面很多用if else就比较费劲了...改造下,新建一个conf.py在里边定义所有的URL模型
if userUrl == '/index/':
return [bytes('<h1>index</h1>',encoding='utf-8')]
elif userUrl == '/login/':
return [bytes('<h1>login</h1>',encoding='utf-8')]
elif userUrl == '/logout/':
return [bytes('<h1>logout</h1>',encoding='utf-8')]
else:
return [bytes('<h1>404 no found</h1>',encoding='utf-8')]
''' if __name__ == '__main__':
httpd = make_server('',8000,RunServer)
print('Serving http on port 8000...')
httpd.serve_forever()
server
#!/usr/local/bin/python3
#-*-coding:utf-8 -*-
#Author:Felix Song ;Environment:pycharm 5.0.3(python3.6) def index():
return [bytes('<h1>index</h1>',encoding='utf-8')]
# return [b'<h1>index</h1>']
def login():
return [bytes('<h1>login</h1>',encoding='utf-8')] #第三步改造,路由系统
def routes():
urlpatterns = (('/index',index),('/index',index),('/index',index))
return urlpatterns # #第二步改造,网页对照表
# url = (
# ('/index',index),
# ('/index',index),
# ('/index',index),
# )
conf_url
三、MVC框架(代码的归类)
Model包:对数据库操作
View包:存放html文件
login.html:
Controller包:业务逻辑处理
Account.py:账户相关控制放在这里
Admin.py:后台管理
四、MTV框架(代码的归类)
Model包:对数据库的操作
Template包:存放html文件
View包:业务逻辑
参考:
http://www.cnblogs.com/wupeiqi/articles/4491246.html
http://www.cnblogs.com/wupeiqi/articles/5237672.html
最新文章
- Struts 2的数据校验
- LINUX 磁盘如何分区
- python windows终端窗口下输出编码错误
- Android自定义视图教程
- U口破解指令介绍.
- MapReduce执行过程源码分析(一)&mdash;&mdash;Job任务的提交
- Delphi 把一个ICO转换为BMP
- 在主函数中提示用户输入用户名和密码。另写一方法来判断用户输入是否正确。该方法分别返回一个bool类型的登录结果和和一个string类型的登录信息。如登录成功,返回true及“登录成功”,若登录失败则返回false及“用户名错误”或“密码错误”(使用out参数)
- Redmine管理项目1-自定义属性
- Flask architecture
- C++头文件#include<;bits/stdc++.h>;
- H-W平衡
- Go - method
- JavaScript 30 - 1 学习笔记
- 工具:从一个文件夹中复制jar到另一个文件夹中
- 企业nginx应用实例(功能拆分记录)
- Python之旅Day12 HTML与CSS
- 使用PHPExcel实现Excel文件的导入和导出(模板导出)
- 如何切换pip的源
- POJ 3159 Candies (图论,差分约束系统,最短路)
热门文章
- Exception inside CORBA when accessing a remote bean
- IOS 打包提示错误(ERROR ITMS-90125: ERROR ITMS-90087: ERROR ITMS-90209:)
- ERROR 1366 (HY000): Incorrect string value: &#39;\xB3\xA4\xC9\xB3&#39; for column
- Luogu P1447 [NOI2010]能量采集 数论??欧拉
- Codeforces 140E(排列组合、dp)
- 重写FileUpload控件让它可以显示上传后的文件名
- Java学习知识体系大纲梳理
- Eclipse Debug模式和断点调试
- 准备Kendo UI 开发环境
- The great pleasure in life is doing what people say you cannot do.