今日内容

  1、基于TCP协议(通信循环)

  2、基于TCP协议(连接循环)

  3、粘包问题

  4、模拟SSH实现远程执行命令

服务器端

###############################################################################################

from socket import *
import json
import struct Sever = socket(AF_INET, SOCK_STREAM)
IP = '192.168.13.131'
PORT = 8084
Sever.bind((IP, PORT)) Sever.listen(5)
while True: #这里说明了循环链接问题的实现方式,在Sver.accept()上面加一个循环可以使得在客户端断开连接后,服务器再次回到这里继续处于阻塞状态,等待下一个客户端请求连接
conn, address = Sever.accept()
while True:#这里说明了通信循环问题的实现方式,在data = conn.recv(1024)上面这样服务器可以循环的接收客户端的消息,客户端也是要做相同的循环
try:
data = conn.recv(1024)
num = data.decode('utf-8')[0]
if num == '1':
print('从客户端接收到的数据:', data.decode('utf-8'))
         #解决粘包问题,在数据前面添加一个报头
with open(r'%s' % data[1::].decode('utf-8'), mode='rb') as f:
msg = b''
for i in f.readlines():
msg += i
file_size = len(msg)
header_dic = {
'file_name': 'aaa',
'file_number': '123456',
'file_size': file_size
}
header_json = json.dumps(header_dic)
header = struct.pack('i', len(header_json))
conn.send(header)
conn.send(header_json.encode('utf-8'))
conn.send(msg)
elif num =='2':
header = conn.recv(4)
header_size = struct.unpack('i', header)[0]
header_json = conn.recv(header_size)
header_dic = json.loads(header_json)
total_size = header_dic['file_size']
print(header_dic)
file_msg = b''
recv_size = 0
with open(r'E:\code\GoodDog\day32\文件上传下载之终极版本\服务器端接收', 'wb') as f:
while recv_size < total_size:
data = conn.recv(1024)
recv_size += len(data)
file_msg += data
f.write(data)
except ConnectionResetError:
break
conn.close()

################################################################################################

客户端

#####################################################################################################

from socket import *
import json
import struct PC = socket(AF_INET,SOCK_STREAM)
IP='192.168.13.131'
PORT = 8084
PC.connect((IP,PORT)) while True:
msg = input('文件地址:').strip()
if len(msg) == 0:continue
PC.send(msg.encode('utf-8'))
if msg[0] == '1':
header = PC.recv(4)
header_size = struct.unpack('i', header)[0]
header_json = PC.recv(header_size)
header_dic = json.loads(header_json)
total_size = header_dic['file_size']
print(header_dic)
file_msg = b''
recv_size = 0
with open(r'E:\code\GoodDog\day32\文件上传下载之终极版本\客户端接收.txt', 'wb') as f:
while recv_size < total_size:
data = PC.recv(1024)
recv_size+=len(data)
file_msg+=data
f.write(data)
elif msg[0] == '2':
with open(r'E:\code\GoodDog\day32\文件上传下载之终极版本\客户端接收.txt', mode='rb') as f:
msg = b''
for i in f.readlines():
msg += i
file_size = len(msg)
print(file_size)
header_dic = {
'file_name': 'aaa',
'file_number': '123456',
'file_size': file_size
}
header_json = json.dumps(header_dic)
header = struct.pack('i', len(header_json))
PC.send(header)
PC.send(header_json.encode('utf-8'))
PC.send(msg)
print(msg)

#####################################################################################################

模拟SSH实现远程执行命令

服务器

#######################33##############################################################################

import socket
import subprocess
import struct
Sever = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip = '127.0.0.7'
port = 3312
Sever.bind((ip,port)) Sever.listen(2)
while True:
conn,address = Sever.accept()
while True:
try:
msg = conn.recv(1024)
obj = subprocess.Popen(r'%s'%msg.decode('utf-8'), shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout = obj.stdout.read()
stderr = obj.stderr.read()
header = struct.pack('i', len(stdout) + len(stderr))
conn.send(header)
conn.send(stdout)
conn.send(stderr)
except ConnectionResetError:
break
conn.close() Sever.close()

#######################################################################################################

客户端

################################################################################################################

import socket
import struct
PC= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip = '127.0.0.7'
port = 3312
PC.connect((ip,port))
while True:
m = input('请输入:').strip()
PC.send(m.encode('utf-8'))
header = PC.recv(4)
total_size = struct.unpack('i',header)[0]
cmd_res = b''
recv_size = 0
while recv_size < total_size:
date = PC.recv(1024)
recv_size+=len(date)
cmd_res+=date
print(cmd_res.decode('gbk'))

################################################################################################################

05-11 17:13