day46

wbe請求流程

瀏覽器---DNS---淘寶服務器處理請求返回HTML頁面

根DNS服務器---頂級DNS服務器---權威DNS---二級DNS服務器
請求頭:   
GET / HTTP/1.1
Host: 127.0.0.1:8080            #主機名
Connenction: keep-alive         #保持鏈接的存活
Cache0-Control: max-age=0       #0表明永不失效,失效時間    
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like                 Gecko) Chrome/75.0.3770.100 Safari/537.36  #瀏覽器內核
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;
       q=0.8,application/signed-exchange;v=b3  #瀏覽器能夠接收的數據類型  
Accept-Encoding: gzip, deflate, br  #編碼
Accept-Language: zh-CN,zh;q=0.9   #語言

   
請求體: #請求體有兩個換行


響應頭: HTTP/1.1 200 ok
   

響應體: #響應體有兩個換行

   
#ps: http:默認端口是80 https:協議默認端口是443

狀態碼:
   2xx: 200 (ok)
   3xx: 302 (表示重定向) 304(not modified)  
   4xx: 404 (not found) 403(forbidden 禁止訪問)
   5xx: 500 (服務器代碼錯誤) 502(網關錯誤 bad gateway)    

自定義web框架

第一步: sokect 服務
   
第二步: uri和函數的對應關係(專業名詞路由系統)
       
第三步: 將html代碼和MySQL的數據進行融合(專業名詞模板渲染)
   
uri: 統一資源標識符 是一個用於標識某一互聯網資源名稱的字符串      
    Web上可用的每種資源 -HTML文檔、圖像、視頻片斷、程序等 - 由一個通用資源標識符(Uniform Resource      Identifier, 簡稱"URI")進行定位。
       

DJango簡介

使用命令行建立Django服務:

diango-admin startproject mysite(項目名)

python3 manage.py runserver 128.0.0.1:8090
   
setings.py: 用戶自定義的各類配置
urls.py: 路由文件
wsgi.py: 啓動socket服務端的文件
mange.py: 管理文件 python mange.py 各類命令
static: js, css, img, 等靜態文件的存放位置

Django建立完成之後
配置模板文件路徑:
   'DIRS':[os.path.join(BASE_DIR,'templates')]
配置靜態資源的文件路徑:
   STATIC_URL = '/static/'
   STATICFILES_DIRS = (
  os.path.join(BASE_DIR,'static')
  )
註釋中間件
MIDDLEWARE = [
   'django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   #'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse, render


def tool(sql,*args):
    import pymysql
    conn = pymysql.connect(host='127.0.0.1', user='root', password='456', db='db2', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql)
    res = cursor.fetchall()
    print(res)
    return res

def index(request):
    sql = 'select * from userInfo'
    userInfo = tool(sql)
    return render(request,'index.html',{'userinfo': userInfo})

def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        #list 套 dict
        sql = 'select * from userInfo'
        userInfo = tool(sql)
        for user in userInfo:
            if (user.get('name') == request.POST.get('username')) and (user.get('pwd') == request.POST.get('password')):
                return  render(request, 'index.html')
        return  render(request,'login.html',{'erroinfo': '用戶名或密碼錯誤'})


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', index),
    url(r'^login/', login),
    url(r'^', login),
]
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用戶登陸--BBS</title>
    <link rel="stylesheet" href="../static/css/reset.css">
    <style>
        body{
            background-color: #e0e4e8;
        }
        .main{
            position: absolute;
            top: 80px;
            width: 300px;
            left: calc(35%);
            border: 1px solid #eeeeee;
            box-shadow: 0 0 2px #cccccc;
            padding: 40px 60px 0 60px;
            background-color: #ffffff;
        }
        .header h2{
            text-align: center;
        }
        .header a{
            display: block;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-image: url("../static/image/微信圖片_20190613085655.png");
            margin: 10px auto 30px auto;
        }
        .middle form input{
            width: 300px;
            border: 1px solid #ffffff;
            box-shadow: 0 0 1px #cccccc;
            margin-bottom: 10px;
        }
        .middle form a{
            display: block;
            font-size: 12px;
            color: cornflowerblue;
            padding-left: 240px;
            user-select: none;
            margin-bottom: 30px;
        }
        .middle form button{
            width: 80px;
            background-color: cornflowerblue;
            color: white;
            border: none;
            border-radius: 3px;
            margin: 20px 110px 70px ;
        }
        .erro{
            height: 10px;
            font-size: 10px;
            font-weight: 400;
            color: red;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="header">
            <h2>用戶登陸</h2>
            <a></a>
        </div>
        <div class="middle">
            <form action="/login/" method="post">
                <input type="text" name="username" placeholder="登陸用戶名">
                <a>忘記用戶名</a>
                <input type="password" name="password" placeholder="密碼">
                <a>忘記密碼</a>
                <h6 class="erro">{{erroinfo}}</h6>
                <button type="submit" value="登陸">登陸</button>
            </form>
        </div>
    </div>
</body>
</html>
相關文章
相關標籤/搜索