网络知识补充
1、CS架构与BS架构
CS:客户端,服务端
BS:浏览器,服务器
2、tcp 三次握手,四次挥手
https://img-blog.csdnimg.cn/0f9e9e63de954db28ac9d4275d748567.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5LiA5Y-q5Zyw55OcYQ==,size_17,color_FFFFFF,t_70,g_se,x_16
3、数据包传输过程
https://img-blog.csdnimg.cn/5920cf77b04d474fa8d0ce246adb33dd.gif
基于TCP协议的套接字
server端:
from socket import *
phone = socket(AF_INET,SOCK_STREAM)
phone.bind(('127.0.0.1',8899))
phone.listen(5)
while True:
conn,client_add = phone.accept()
print(conn)
print('客户端的ip和端口',client_add)
client端:
from socket import *
phone = socket(AF_INET,SOCK_STREAM)
phone.connect(('127.0.0.1',8899))
while True:
msg = input('请输入命令:').strip()
if len(msg) == 0:
continue
phone.send(msg.encode('utf-8'))
data = phone.recv(1024)
print('接收到服务器发来的:',data.decode('utf-8'))
phone.close()
基于UDP协议的套接字
服务器:
from socket import *
phone = socket(AF_INET,SOCK_DGRAM)
while True:
choice = input('请输入传输内容:').strip()
phone.sendto(choice.encode('utf-8'),('127.0.0.1',8081))
func=phone.recv(1024)
print(func.decode('utf-8'))
客户端
from socket import *
phone = socket(AF_INET,SOCK_DGRAM)
while True:
choice = input('请输入传输内容:').strip()
phone.sendto(choice.encode('utf-8'),('127.0.0.1',8081))
func=phone.recv(1024)
print(func.decode('utf-8'))
TCP沾包问题 and 远程执行命令功能
服务端:
import json
import struct
from socket import *
import subprocess
import string
import _json
phone = socket(AF_INET,SOCK_STREAM)
phone.bind(('127.0.0.1',8899))
phone.listen(5)
while True:
conn,client_add = phone.accept()
while True:
try:
cmd = conn.recv(1024)
if len(cmd) ==0:
break
obj = subprocess.Popen(
cmd.decode('utf-8'),
shell = True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout_res = obj.stdout.read()
stderr_res = obj.stderr.read()
total_size = len(stderr_res) + len(stdout_res)
header_dir = {
"filename":'a.txt',
"total_size":total_size,
"md5":'123123ass123'
}
json_str = json.dumps(header_dir)
json_str_bytes = json_str.encode('utf-8')
x = struct.pack('i',len(json_str_bytes))
conn.send(x)
conn.send(json_str_bytes)
conn.send(stdout_res)
conn.send(stderr_res)
except Exception:
break
客户端:
import struct
from socket import *
import json
client = socket(AF_INET,SOCK_STREAM)
client.connect(('127.0.0.1',8899))
while True:
cmd = input('请输入命令:').strip()
if len(cmd) == 0:
continue
client.send(cmd.encode('utf-8'))
x=client.recv(4)
if not len(x) == 4:
continue
header_len = struct.unpack('i',x)[0]
json_str_bytes = client.recv (header_len)
json_str=json_str_bytes.decode('utf-8')
header_dic = json.loads(json_str)
print(header_dic)
total_size = header_dic["total_size"]
recv_size = 0
while recv_size < total_size:
recv_data = client.recv(1024)
recv_size += len(recv_data)
print(recv_data.decode('gbk'),end='')
else:
print()
socketserver多并发
基于TCP的多并发
import socketserver
class MyRequestHandle(socketserver.BaseRequestHandler):
def handle(self):
print(self.request)
print(self.client_address)
while True:
try:
data = self.request.recv(1024)
if len(data) == 0:
break
self.request.send(data.upper())
except Exception:
break
self.request.close
s = socketserver.ThreadingTCPServer(('127.0.0.1',8888),MyRequestHandle)
s.serve_forever()
from socket import *
phone = socket(AF_INET,SOCK_STREAM)
phone.connect(('127.0.0.1',8888))
while True:
msg = input('请输入命令:').strip()
if len(msg) == 0:
continue
phone.send(msg.encode('utf-8'))
data = phone.recv(1024)
print('接收到服务器发来的:',data.decode('utf-8'))
phone.close()
基于UDP的多并发
from socket import *
phone = socket(AF_INET,SOCK_DGRAM)
while True:
choice = input('请输入传输内容:').strip()
phone.sendto(choice.encode('utf-8'),('127.0.0.1',8888))
func=phone.recvfrom(1024)
print(func)
import socketserver
class MyRequestHandle(socketserver.BaseRequestHandler):
def handle(self):
client_data = self.request[0]
server = self.request[1]
client_add = self.client_address
print('客户端发来的数据%s'%client_data)
server.sendto(client_data.upper(),client_add)
s = socketserver.ThreadingUDPServer(('127.0.0.1',8888),MyRequestHandle)
s.serve_forever()
|