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>
 
原文地址:https://www.cnblogs.com/zhuqihui/p/11160970.html