web框架和Django框架的初識

 1,web框架的原理html

1.1>c/s架構和b/s架構python

  1>c/s客戶端模式web

  2>B/S瀏覽器模式-----web開發(web開發開的是B/S架構)django

1.2>web開發的本質flask

  1.2.1>互聯網上兩臺機器之間的通訊:(1)IP,(2)端口,(3)協議瀏覽器

  1.2.2>協議:(1)HTTP默認端口是80,(2)HTTPS默認端口是443服務器

  1.2.3>HTTP get請求格式架構

  1.2.4>HTTP相應格式app

 

1.3>瀏覽器輸入URL回車返回頁面,這之間都經歷了什麼?框架

  (1)域名 ->DNS解析 ->IP地址 ->服務端 ->返回消息 ->瀏覽器

  (2)瀏覽器  <==> 服務器

  (3)服務器吧寫好的HTML頁面,返回給;瀏覽器按照HTML格式進行渲染(顯示)

2,python中的web框架的分類

  2.1>收發socket消息,按照HTTP協議解析消息 

  2.2>字符串替換

  2.3>業務邏輯處理

  本身實現2.1,2.2,2.3的是tornado框架

  本身實現2.2,2.3使用別人的2.1是Django框架

  本身實現2.3使用別人的2.1,2.2是flask

  另外一個維度 的框架分類:1,Django  2,其餘

3,Django的安裝

  3.1>django注意版本LTS

  3.2>安裝命令:pip install Django == 1.11.11(卸載Django:pip uninstall django)

  3.3>建立第一個Django項目

    3.3.1>命令行建立:django-admin startproject 項目名

    3.3.2>在pycharm:File ->new Project ->左側選第二項,右側第一項是路徑,第二頁是python版本,在新的pycharm窗口打開新建的Django項目

  3.4>啓動django項目

    3.4.1>在命令行啓動:在django項目的跟目錄下,執行下面的命令

      python manage.py runserver

      python manage.py runserver 8080

      中止:Ctrl + c

    3.402>pycharm啓動:點綠色的三角,注意左側框中的名字必定鑰匙項目名稱!

 

 

 4,模擬web服務器

4.1>普通版本

import socket
sk = socket.socket()

sk.bind(("127.0.0.1",8080))
sk.listen()

while 1:

    conn,addr = sk.accept()

    data = conn.recv(1024)

    #接收到的是一個byte類型的數據須要轉化成字符串,注意編碼

    data_str = str(data,encoding="utf-8")

    #對字符串進行切割操做

    first_line = data_str.split("\r\n")[0]
    #對請求按空格切割

    url = first_line.split(" ")[1]
    
    if url == "/xuexue/":
         msg = b"hello"
    elif url == "/xiaoxue/":
          msg = b"miss"
    else:
        msg = b"404 not found !"
    conn.send(b"HTTP/1.1 200 Ok\r\n\r\n")
    conn.send(msg)
    conn.close()

客戶端fade是個啥

'''GET /yingying/ HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9

'''

4.2>模擬web服務器的函數版本

import socket
sk = socket.socket()
sk.bind(("127.0.0.1",8080))
sk.listen()
def xuexue(url):   #這個哪吒說必須傳url參數,可是不傳參數也能夠啊
     data = b"hello"
     return data

def xiaoxue(url):

    data = "miss"

     return bytes(data,encoding="utf-8")

while 1:

    conn,addr = sk.accept()

    data = conn.recv(1024)

    data_str = str(data,encoding="utf-8")

    first_line = data_str.split("\r\n")[0]

    url = first_line.split(" ")[1]

    if url == "/xuexue/":

        msg = xuexue(url)

     elif url == "/xiaoxue/":

        msg = xiaoxue()

      else:

        msg = b"404 not found !"

    conn.send(b"HTTP/1.1 200 ok\r\n\r\n")

    conn.send(msg)

    conn.close()

4.3>模擬web服務器的反射進階版

import socket
sk = socket.socket()
sk.bind(("127.0.0.1",8080))
sk.listen()

def xuexue(url):

    return b"hello"

def xiaoxue(url):

    data = "miss"

    return bytes(data,encoding="utf-8")

url_func = [

    ("/xuexue/",xuexue),
    ("/xiaoxue/",xiaoxue)   
]

while 1:

    conn,addr = sk.accept()

    data = conn.recv(1024)

    data_str = str(data,encoding="utf-8")

    first_line = data_str.split("\r\n")[0]

    url = first_line.split(" ")[1]

    for el in url_func:

            if el[0] == url

                msg = el[1](url)

    else:

            msg = b"404 not found"

    conn.send(b"HTTP/1.1 200 ok\\r\n\r\n")

    conn.send(msg)

    sonn.close()

4.4> 靜態網頁版本

import socket
sk = socket.socket()
sk.bind(("127.0.0.1",8080))
sk.listen()
def xuexue(url):
    data = "hello"
    return bytes(data,encoding="utf-8")

def xiaoxue(url):

    data = "miss"

    return bytes(data,encoding="utf-8")

def login(url):

    with open("login.html","rb") as f:

        data = f.read()

    return data

url_func = [
    ("/xuexue/",xuexue),
    ("/xiaoxue/",xiaoxue),           
    ("/login/",login)
]

while 1:

    conn,addr = sk.accept()
    
    data = conn.recv(8192)
    data_str = str(data,encoding="utf-8")#bytes類型轉化成字符串能夠直接轉化,而且加一個編碼.
    first_line = data_str.split("\r\n")[0]

    url = first_line.split(" ")[1]

    for el in url_func:

         if el[0] ==url:

            msg = el[1](url)

    else:

    
        msg = b"404 not found !"

    conn.send(b"HTTP/1.1 200 ok\r\n\r\n")

    conn.send(msg)

    conn.close()

4.5>動態網頁版本

import socket
import time
sk = socket,socket()
sk.bind(("127.0.0.1",8080))
sk.listen()
def xuexue(url):
    data = "hello"
    return bytes(data,encoding="utf-8")
def xiaoxue(url):
    data = "miss"
    return bytes(data,encoding="utf-8")
def login(url):
    with open("login.html","r") as f:
        data = f.read()
    now = str(time.time())
    data = data.replace("@@xx@@",now)
    return bytes(data,encoding="utf-8")
url_func = [
    ("/xuexue/",xuexue),       
    ("/xiaoxue/",xiaoxue),
    ("/login/",login)
]
while 1:
    
    conn,addr = sk.accept()
    data = conn.recv(8192)

    data_str = bytes(data,encoding="utf-8")
    
    first_line = data_str.split("\r\n")[0]
    url = first_ine.split(" ")[1]
    for el in url_func:
        if el[0] == url:
                msg = el[1](url)
    else:
        msg = b"404 not found !"
    conn.send(b"HTTP/1.1 200 ok\r\n\r\n")
    conn.send(msg)

 

相關文章
相關標籤/搜索