到底什么是HTTP协议,我们可以通过简单的服务器搭建来进行验证一下。 我不喜欢介绍枯燥的理论,更喜欢直接上手敲代码,理论留给那些科学家去研究吧!为了验证,直接上代码验证。
import socket
def service_client(new_socket):
request =new_socket.recv(1024)
print(request)
response ="HTTP/1.1 200 OK\r\n"
response =response+"\r\n"
response =response+ "hahahha"
new_socket.send(response.encode("utf-8"))
new_socket.close()
def main():
"""整体控制"""
tcp_server_socket =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcp_server_socket.bind(("",7890))
tcp_server_socket.listen(128)
while True:
new_socket,client_addr =tcp_server_socket.accept()
service_client(new_socket)
tcp_server_socket.close()
if __name__ == '__main__':
main()
我们打开浏览器访问 127.0.0.1:7890 我们可以看到浏览器可以返回访问到我们预先设定好的字符串,在终端服务端我们可以看到输出结果
b'GET / HTTP/1.1\r\nHost: 127.0.0.1:7890\r\nConnection: keep-alive\r\nCache-Control: max-age=0\r\nsec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"\r\nsec-ch-ua-mobile: ?0\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\nSec-Fetch-Site: none\r\nSec-Fetch-Mode: navigate\r\nSec-Fetch-User: ?1\r\nSec-Fetch-Dest: document\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: zh-CN,zh;q=0.9\r\nCookie: COOKIE_SESSION=1316098_0_0_5_0_2_0_0_0_1_19_1_0_0_0_0_0_0_1615973371%7C5%230_0_1615973371%7C1\r\n\r\n'
b'GET /favicon.ico HTTP/1.1\r\nHost: 127.0.0.1:7890\r\nConnection: keep-alive\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nsec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"\r\nsec-ch-ua-mobile: ?0\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36\r\nAccept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8\r\nSec-Fetch-Site: same-origin\r\nSec-Fetch-Mode: no-cors\r\nSec-Fetch-Dest: image\r\nReferer: http://127.0.0.1:7890/\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: zh-CN,zh;q=0.9\r\nCookie: COOKIE_SESSION=1316098_0_0_5_0_2_0_0_0_1_19_1_0_0_0_0_0_0_1615973371%7C5%230_0_1615973371%7C1\r\n\r\n'
浏览器可以顺利的访问到我们的预设值,进而证明了HTTP协议。
TCP三次握手,四次挥手
很讨厌那些详细介绍的理论,简单来说就两句话,TCP三次握手就是客户端向服务端发数据开始工作,四次挥手就是客户端关闭调用close()客户端和服务器交流四次(客户端关闭发送close())> (服务端(关闭接收)) >(客户端(关闭接收))>(服务端(关闭发送))>(结束会话),这就是整个挥手流程
import socket
import re
def service_client(new_socket):
request =new_socket.recv(1024).decode("utf-8")
request_lins = request.splitlines()
print("")
print(">"*20)
print(request_lins)
ret =re.match(r"[^/]+(/[^ ]*)",request_lins[0])
if ret:
file_name=ret.group(1)
print("*"*50,file_name)
response ="HTTP/1.1 200 OK\r\n"
response =response+"\r\n"
f =open(r"./demo/index.html","rb")
html_cont =f.read()
f.close()
new_socket.send(response.encode("utf-8"))
new_socket.send(html_cont)
new_socket.close()
def main():
"""整体控制"""
tcp_server_socket =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcp_server_socket.bind(("",7890))
tcp_server_socket.listen(128)
while True:
new_socket,client_addr =tcp_server_socket.accept()
service_client(new_socket)
tcp_server_socket.close()
if __name__ == '__main__':
main()
我们或许只能看到文字,并不能访问图片,对于改进代码如下:
如果我们要是想访问文件的话,那么就需要改进一下代码,如下:
import socket
import re
def service_client(new_socket):
request =new_socket.recv(1024).decode("utf-8")
request_lins = request.splitlines()
print("")
print(">"*20)
print(request_lins)
ret =re.match(r"[^/]+(/[^ ]*)",request_lins[0])
if ret:
file_name=ret.group(1)
print("*"*50,file_name)
response ="HTTP/1.1 200 OK\r\n"
response +="\r\n"
f =open(r"./demo"+file_name,"rb")
html_cont =f.read()
f.close()
new_socket.send(response.encode("utf-8"))
new_socket.send(html_cont)
new_socket.close()
def main():
"""整体控制"""
tcp_server_socket =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcp_server_socket.bind(("",7890))
tcp_server_socket.listen(128)
while True:
new_socket,client_addr =tcp_server_socket.accept()
service_client(new_socket)
tcp_server_socket.close()
if __name__ == '__main__':
main()
我强烈的推荐广大朋友们用Linux进行网络收发的服务,比如作者在运行代码的时候在Windows里就遇到了这个报错问题
PermissionError: [Errno 13] Permission denied: './demo/'
经过问题排查,我们发现是访问权限的问题,访问权限不够,唉,找了好长时间终于解决了,右击访问文件夹属性,折腾了好长时间,最终我选择了Linux,在Linux平台解决文件夹权限的问题十分简单。一条指令就可以解决
chmod +rxw demo
运行这条指令的话在Ubuntu下可以完全的运行文件了,至此简单的服务器搭建成功,全剧终!!!!
|