开发者学堂课程【Python 入门 2020年版:HTTP 请求头】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/639/detail/10478
HTTP 请求头
代码:
import socket
server_socket=socket.socket(socket.AF_INEF,socket.SOCK_STREAM)
server_socket.bind((‘0.0.0.0’,8090))
server_socket.listen(128)
while True:
client_socket,client_addr=server_socket.accept()
data=client_socket.recv(1024).decode(‘utf8’)
print(‘接收到{}的数据{}’.format(client_addr[0],data))
client_socket.send(‘HTTP/1.1 200 OK\n’.encode(‘utf8’))
client_socket.send(‘content-type:text/html’.encode(‘utf8’))
client_socket.send(‘\n’.encode(‘utf8’))
client_socket.send(‘<h1 style=“color:red”>hello world
</h1>.encode(‘utf8’))
运行后出现:
这个就是请求头
代码中的 data 就是请求头:
import socket
server_socket=socket.socket(socket.AF_INEF,socket.SOCK_STREAM)
server_socket.bind((‘0.0.0.0’,8090))
server_socket.listen(128)
while True:
client_socket,client_addr=server_socket.accept()
data=client_socket.recv(1024).decode(‘utf8’)
print(‘接收到{}的数据{}’.format(client_addr[0],data))
“””
# GET 请求方式,GET/POST/PUT/DELETE... ...
# / index.html 请求的路径
#HTTP/1.1 HTTP 版本号
GET / index.html HTTP/1.1
Host: 192.168.31. 199: 8090
Upgrade- Insecure- Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.
Accept: text/html, application/xhtml+xml, application/xm1;q=0.9, image/webp, image/apng, */*;q=0.8, application
Accept- Encoding: gzip, deflate
Accept-Language: zh-CN, zh;q=0.9,en;q=0.8
Connection: close
“””
client_socket.send(‘HTTP/1.1 200 OK\n’.encode(‘utf8’))
client_socket.send(‘content-type:text/html’.encode(‘utf8’))
client_socket.send(‘\n’.encode(‘utf8’))
client_socket.send(‘<h1 style=“color:red”>hello world
</h1>.encode(‘utf8’))
输入 192.168.31.199:8090/index.html?Name=jack&age=18,再给服务器发送一个请求看看,发现数据又变多了。
# GET 请求方式,GET/POST/PUT/DELETE... ...
# / index.html?Name=jack&age=18 请求的路径以及请求参数
#HTTP/1.1 HTTP 版本号
GET / index.html?Name=jack&age=18 HTTP/1.1
Host: 192.168.31. 199: 8090
#请求的服务器地址
Upgrade- Insecure- Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80. 0.3987.122 Safari/537.36
#User-Agent 简称 UA,用户代理,最开始设计的目的,是为了能从请求里头辨识浏览器的类型。但是现在加的越来越多了看不出来自己用的是哪个浏览器了。
Accept: text/html, application/xhtml+xml, application/xm1;q=e.9, image/webp, image/apng, */*;q=0.8, application
Accept- Encoding: gzip, deflate
Accept-Language: zh-CN, zh;q=0.9,en;q=0.8
Connection: close