目錄:
1.框架各組成部分介紹
2.框架啟動(dòng)文件
3.路徑與視圖函數(shù)映射關(guān)系
4.視圖函數(shù)
5.模板文件
6.數(shù)據(jù)庫(kù)操作文件
1.框架各組成部分介紹
簡(jiǎn)易的web框架大概有以下部分組成:
main.py: 啟動(dòng)文件,封裝了socket
1. urls.py:路徑與視圖函數(shù)映射關(guān)系 ---- url控制器
2. views.py:視圖函數(shù)丰歌,固定有一個(gè)形式參數(shù):environ ---- 視圖函數(shù)
3. templates文件夾:html文件 ---- 模板
4. models.py:在項(xiàng)目啟動(dòng)前翔脱,在數(shù)據(jù)庫(kù)中創(chuàng)建表結(jié)構(gòu) ---- 與數(shù)據(jù)庫(kù)相關(guān)
2.框架啟動(dòng)文件
main.py做為整個(gè)框架的啟動(dòng)文件澈圈,組要負(fù)責(zé)socket封裝
from wsgiref.simple_server import make_server
from urls import url_pattern
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
print("PATH",environ.get("PATH_INFO"))
# 當(dāng)前請(qǐng)求路徑
path = environ.get("PATH_INFO")
func = None
for item in url_pattern:
if path == item[0]:
func = item[1]
break
if func:
return [func()]
else:
return [b"404!"]
return [b'<h1>Hello, web!</h1>']
httpd = make_server('', 8080, application)
print('Serving HTTP on port 8080...')
# 開始監(jiān)聽HTTP請(qǐng)求:
httpd.serve_forever()
3.路徑與視圖函數(shù)映射關(guān)系
urls.py:路徑與視圖函數(shù)映射關(guān)系 ---- url控制器
from views import *
# 試圖函數(shù)
url_pattern = [
("/login", login),
("/reg", reg),
("/index", index),
("/timer",timer),
("/favicon.ico", favi)
]
4.視圖函數(shù)
views.py:視圖函數(shù)岩灭,固定有一個(gè)形式參數(shù):environ ---- 視圖函數(shù)
import datetime
def login():
with open("templates/login.html", "rb") as f:
data = f.read()
return data
def index():
with open("templates/index.html", "rb") as f:
data = f.read()
return data
def favi():
with open("templates/favicon.ico", "rb") as f:
data = f.read()
return data
def reg():
with open("templates/reg.html", "rb") as f:
data = f.read()
return data
def timer():
now = datetime.datetime.now().strftime("%y-%m-%d %X")
return now.encode("utf-8")
5.模板文件
templates文件夾:html文件 ---- 模板
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<img src='https://t11.baidu.com/it/u=2162308300,96045043&fm=58'>
<a >click</a>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8800/" method="post">
username <input type="text" name="user">
password <input type="password" name="pwd">
<input type="submit">
</form>
</body>
</html>
reg.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h4>注冊(cè)頁(yè)面</h4>
</body>
</html>
6.數(shù)據(jù)庫(kù)操作文件
models.py:在項(xiàng)目啟動(dòng)前涤姊,在數(shù)據(jù)庫(kù)中創(chuàng)建表結(jié)構(gòu) ---- 與數(shù)據(jù)庫(kù)相關(guān)
只能做表相關(guān)的操作籽懦,使用前财骨,應(yīng)先創(chuàng)建好對(duì)應(yīng)的數(shù)據(jù)庫(kù)
import pymysql
# 生成用戶表
#連接數(shù)據(jù)庫(kù)
conn = pymysql.connect(host='127.0.0.1',port= 3306,user = 'root',passwd='',db='web') #db:庫(kù)名
#創(chuàng)建游標(biāo)
cur = conn.cursor()
sql='''
create table userinfo(
id INT PRIMARY KEY ,
name VARCHAR(32) ,
password VARCHAR(32)
)
'''
cur.execute(sql)
#提交
conn.commit()
#關(guān)閉指針對(duì)象
cur.close()
#關(guān)閉連接對(duì)象
conn.close()