web框架入門

1. HTTP協議

超文本傳輸協議:
1.四大特性
    1.基於TCP/IP之上做用於應用層
    2.基於請求響應
    3.無狀態   cookies  session  token
    4.無鏈接
        長鏈接  websocket(HTTP協議的大補丁)
        
2.數據格式
    請求格式
        請求首行(請求方式,協議版本...)
        請求頭(一大堆k:v鍵值對)
        \r\n
        請求體(真正的數據 發post請求時纔有 若是是get請求不會有)
        響應首行
        響應頭
        \r\n
        響應體
        
3.響應狀態碼
    用特定的數字表示一些意思
    1xx:服務端已經成功接收到了你的數據 正在處理 你能夠繼續提交其餘數據
    2xx:服務端成功響應(200請求成功)
    3xx:重定向
    4xx:請求錯誤(404:請求資源不存在  403:拒絕訪問)
    5xx:服務器內部錯誤(500)

2. 請求方式

get請求:朝別人要數據
post請求:向別人提交數據(eg:用戶登陸)

url:統一資源定位器

3. 純手寫web框架

import socket

server = socket.socket()
server.bind(('127.0.0.1', 8001))
server.listen(5)

'''
請求首行
b'GET / HTTP/1.1\r\n
請求頭
Host: 127.0.0.1:8888\r\n
Connection: keep-alive\r\n
Cache-Control: max-age=0\r\n
Upgrade-Insecure-Requests: 1\r\n
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36\r\n
Sec-Fetch-Mode: navigate\r\n
Sec-Fetch-User: ?1\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\n
Sec-Fetch-Site: none\r\n
Accept-Encoding: gzip, deflate, br\r\n
Accept-Language: zh-CN,zh;q=0.9\r\n
\r\n
請求體
'

'''

while True:
    conn, addr = server.accept()
    data = conn.recv(1024)
    # print(data)
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
    data = data.decode('utf8')
    current_path = data.split('\r\n')[0].split(' ')[1]
    # print(current_path)
    if current_path == '/index':
        with open(r'F:\實習\python\1018\web框架推導\templates\index.html', 'rb') as f:
            conn.send(f.read())
    elif current_path == '/login':
        conn.send(b'login')
    else:
        conn.send(b'404 error')
    conn.close()

4. 基於wegiref模塊

# urls.py  路由與視圖函數對象關係
# views.py  放的是視圖函數(處理業務邏輯的)
# templates  模板文件夾(一堆html文件)

# urls.py
from views import *

urls = [
    ('/index',index),
    ('/login',login),
    ('/xxx',xxx),
    ('/get_time',get_time),
    ('/get_user',get_user),
    ('/get_db',get_db)
]


# views.py
import datetime
from jinja2 import Template

def index(env):
    return 'index'

def login(env):
    return 'login'

def error(env):
    return '404 error'

def xxx(env):
    return 'xxx'

def get_time(env):
    time = datetime.datetime.now().strftime('%Y-%m-%d %X')
    with open(r'F:\實習\python\1018\web框架推導\templates\get_time.html','r',encoding='utf8')as f:
        data = f.read()
    data = data.replace('$$time$$',time)
    return data

def get_user(env):
    d = {'name':'aa','pwd':'123','hobby':['read','running','music']}
    with open(r'F:\實習\python\1018\web框架推導\templates\get_user.html','r',encoding='utf8') as f:
        data = f.read()
    temp = Template(data)
    res = temp.render(user=d)   # 將字典d傳遞給前端頁面 頁面上經過變量名user就可以獲取到該字典
    return res


# 基於wsgiref.py
import pymysql
def get_db(env):
    conn = pymysql.connect(
        host = '127.0.0.1',
        user = 'root',
        password = 'root',
        database = 'db926',
        port = 3306,
        charset = 'utf8',
        autocommit = True,
    )
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    sql = 'select * from bank'
    cursor.execute(sql)
    res = cursor.fetchall()
    print(res)
    with open(r'F:\實習\python\1018\web框架推導\templates\get_db.html','r',encoding='utf8') as f:
        data = f.read()
    temp = Template(data)
    res1 = temp.render(info_list = res)
    return res1

from wsgiref.simple_server import make_server
from urls import urls
from views import error


def run(env, response):
    """
    :param env: 請求相關的全部數據
    :param response: 響應相關的全部數據
    :return
    """
    response('200 OK', [])
    current_path = env.get('PATH_INFO')
    func = None
    # for循環 匹配後綴
    for url in urls:
        if current_path == url[0]:
            func = url[1]   # 一旦匹配成功 就將匹配到的函數名賦值給func變量
            break   # 主動結束匹配
    # 判斷func是否有值
    if func:
        res = func(env)
    else:
        res = error(env)
    return [res.encode('utf8')]


if __name__ == '__main__':
    server = make_server('127.0.0.1', 8000, run)
    # 實時監聽該地址  只要有客戶端來鏈接 統一交給run函數去處理
    server.serve_forever()

5. 動靜態網頁

靜態網頁:數據是寫死的 萬年不變html

動態網頁
數據是實時獲取的
eg:
1.後端獲取當前時間展現到前端
2.後端獲取數據庫中的數據展現到前端前端

後端獲取的數據 傳遞給html頁面 >>>: 模板的渲染python

jinja2mysql

模板語法(極其貼近python後端語法)web

<p>{{ user }}</p>
        <p>{{ user.name }}</p>
        <p>{{ user['pwd'] }}</p>
        <p>{{ user.get('hobby') }}</p>
        
        
        {% for user_dict in user_list %}
            <tr>
                <td>{{ user_dict.id }}</td>
                <td>{{ user_dict.name }}</td>
                <td>{{ user_dict.pwd }}</td>
            </tr>
        {% endfor %}

6. python三大主流web框架

Django:
    大而全 自帶的功能特別特別多 相似於航空母艦
    有時候過於笨重
    
Flask
    小而精 自帶的功能特別特別少 相似於遊騎兵
    第三方的模塊特別特別多,若是將flask第三方模塊所有加起來 徹底能夠超過django
    比較依賴於第三方模塊
    
Tornado
    異步非阻塞
    牛逼到能夠開發遊戲服務器
        
        
A:socket部分
B:路由與視圖函數對應關係
C:模板語法
    
Django:
    A用的別人的  wsgiref
    B本身寫的
    C本身寫的
Flask
    A用的別人的  werkzeug(基於wsgiref)
    B本身寫的
    C用的別人的  jinja2
Tornado
    三者全是本身寫的

注意事項
    1.計算機的名稱不能有中文
    2.一個pycharm窗口就是一個項目
    3.項目名裏面儘可能不要用中文
    
django安裝
    pip3 install django==1.11.11
    
驗證django是否安裝成功
    命令行直接敲django-admin
    
一個django項目就相似因而一所大學,而app就相似於大學裏面的學院
django其實就是用來一個個應用的
一個app就至關於一塊獨立的功能
    用戶功能
    管理功能
django支持任意多個app

如何使用:
1.命令行使用
    建立django項目
        django-admin startproject mysite
    啓動django項目
        python manage.py runserver
    建立應用app
        python manage.py startapp app01
            
注意:
    1.新建立的app須要去settings配置文件中手動在TEMPLATES寫配置: os.path.join(BASE_DIR, 'templates')
    2.pycharm只會幫你註冊第一個你在建立項目的時候寫的應用
    3.使用命令行建立django項目 不會自動幫你建立templates文件夾只能本身建立
  

2.pycharm使用
    在啓動django項目的時候 你必定要確保一個端口只有一個django項目
   
項目名
    跟項目名同名的文件夾
        settings.py  暴露給用戶的配置文件
        urls.py  路由與視圖函數對應關係
    應用名
        migrations文件夾  存放數據庫遷移記錄的
        admin.py  django後臺管理
        apps.py  註冊相關
        models.py  模型類 
        tests.py  測試文件
        views.py  存放視圖函數
    templates文件夾  存放html文件
    manage.py  django入口文件
相關文章
相關標籤/搜索