一、手撸web框架
推导框架的演变过程,思路是重点
1、写一个服务端
import socket server = socket.socket() server.bind(('127.0.0.1', 9000)) server.listen(3) while True: conn, addr = server.accept() data = conn.recv(1024) print(data.decode('utf8')) conn.send(b'HTTP//1.1 200 OK \r\n\r\n') conn.send(b'hello django!') conn.close()
注意: conn.send(b'HTTP//1.1 200 OK \r\n\r\n') tcp的流式协议,注意是两个\r\n\r\n
2、在浏览器地址栏中写什么后缀就返回什么内容?
一次正常的访问,后端会收到2个GET请求,第一个GET后面为浏览器端输入的后缀,第二个为/favicon.ico 网站图标
<link rel="icon" href="favicon.ico" type="image/x-icon">
GET /login HTTP/1.1 Host: 127.0.0.1:9000 Connection: keep-alive sec-ch-ua: "Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114" ..... Accept-Language: zh-CN,zh;q=0.9 GET /favicon.ico HTTP/1.1 Host: 127.0.0.1:9000 Connection: keep-alive sec-ch-ua: "Not.A/Brand";v="8", "Chromium";v="114", "Google .... Referer: http://127.0.0.1:9000/login Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9
从上可知想要拿到login需要做一个切割操作
3、
二、
三、
标签:HTTP,GET,server,初识,9000,zh,django,conn From: https://www.cnblogs.com/dgp-zjz/p/17582588.html