基础

# HTTP响应状态码
    10X:服务端已经接受到你的数据了 你可以继续提交数据进行下一步操作
    20X:请求成功(200)
    30X:重定向(301,302)
    40X:请求错误(404)
    50X:服务端错误(500)

# GET请求与POST请求
        GET请求:获取页面资源
        POST请求:朝服务端发送数据(让服务端校验该数据)

一、Web框架本质

  所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端

 

根据不同的路径返回不同的内容

  可以写几个简单页面,然后通过http://127.0.0.1:8080/页面名称  来访问对应的页面测试

import socket

server = socket.socket()  # 默认是TCP协议
server.bind(('127.0.0.1',8080))  # 绑定IP端口
server.listen(5)  # 监听 半链接池:保护计算机安全

while True:
    conn, addr = server.accept()   # 等待连接
    data = conn.recv(1024)         # 接收客户端信息
    # 遵循HTTP协议,给回应的消息加上响应状态行
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
    res = data.decode('utf-8')
    current_path = res.split('\r\n')[0].split(' ')[1]  # 字符串切割,获取路径
    # print(current_path)
    # 根据不同的路径返回不同的内容
    if current_path == '/index':
        # conn.send(b'index')
        with open('templates/111.html','rb') as f:
            conn.send(f.read())
    elif current_path == '/login':
        conn.send(b'login')
    else:
        conn.send(b'404')
    conn.close()

浏览器访问页面请求信息:

  HTTP协议主要规定了客户端和服务器之间的通信格式

  响应相关信息可以在浏览器调试窗口的network标签页中看到。可以根据view信息切割获得需要请求的页面

请求首行
b'GET / HTTP/1.1\r\n  
请求头
Host: 127.0.0.1:8080\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; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36\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
Accept-Encoding: gzip, deflate, br\r\n
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\r\n
Cookie: csrftoken=3vPenhmlRQb8Tvl4okwYM0OZpDCl3P7rbxvfpRDOHJy1zUApw89ugxM6OZSxhIBM\r\n
\r\n
请求体
'
http://127.0.0.1:8080/index
view展开:
python之Djiango框架简介

根据不同的路径返回不同页面请求---函数版

import socket

sk = socket.socket()
sk.bind(('127.0.0.1',8001))
sk.listen()

# 将不同的内容部分封装成函数
def index(url):
    # 读取index.html页面内容
    with open("index.html",'rb',encoding="utf-8") as f:
        s = f.read()
    # 返回字节数据
    return bytes(s,encoding="utf-8")

def login(url):
    with open("login.html",'rb',encoding="utf-8") as f:
        s = f.read()
    return bytes(s, encoding="utf-8")

list1= [
    ("templates/111.html","/index"),
    ("templates/login.html",'/login'),
]

while True:
    # 等待连接
    conn,add = sk.accept()
    data = conn.recv(1024) # 接收客户端发来的消息
    # 从data 中获取路径
    data = str(data,encoding='utf-8') # 把收到的字节类型数据转换成字符串
    print("data>>>",data)
    # 按\r\n切割,url是从浏览器发过来的消息中分离出来的访问路径
    url = data.split("\r\n")[0].split(' ')[1]
    print("url>>>>",url)
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n') #遵循http协议,所以回复的消息也要加状态行
    # 根据不同的路径返回不同内容,reponse是具体的响应体
    func = None
    for i in list1:
        if i[0].split("/")[1] == url:
            func = i[1]
            break
    # print("func>>>",func)
    if func:
        reponse = func(url)
    else:
        reponse = b"404 not found!"

    conn.send(reponse)
    conn.close()

# 还有点问题//TODO
View Code

相关文章:

  • 2021-10-26
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2021-08-15
猜你喜欢
  • 2021-07-03
  • 2022-12-23
  • 2021-06-30
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
相关资源
相似解决方案