服务端:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import subprocess
import struct
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1) # 端口复用
phone.bind(('127.0.0.1', 8090))
phone.listen(10)
while True: # 连接循环
connt , client = phone.accept()
print('starting ....')
print(client)
while True: # 通信循环
try:
# 1.收到命令:
cmd = connt.recv(1024)
if not cmd :continue # 适用于Linux 操作系统
# 2.执行命令:
ojb = subprocess.Popen(cmd.decode('utf-8'),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout = ojb.stdout.read()
stderr = ojb.stderr.read()
# 3.返回命令结果给客户端:
# 第一步 制作固定长度的报头
res = len(stdout + stderr)
total_size = struct.pack('i', res)
# 第二部把报头发给客户端
connt.send(total_size)
# 第三步再发送真实数据
connt.send(stdout)
connt.send(stderr)
except ConnectionResetError: # 适用于windows操作系统
break
connt.close()
phone.close()
客户端:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import struct
phone1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
phone1.connect(('127.0.0.1',8090))
while True:
# 1.发命令
cmd = input('>>>:').strip()
if not cmd:continue
phone1.send(cmd.encode('utf-8'))
# 2.拿到命令并打印结果
# 第一步先收报头
obj=phone1.recv(4)
# 第二步从包头中解析出对真实数据的描述信息(数据真实的长度)
total_size=struct.unpack('i',obj)[0]
# 第三步 接收真实数据
revs_size = 0
data_size = b''
while revs_size < total_size:
data =phone1.recv(1024)
data_size += data
revs_size += len(data)
print(data_size.decode('gbk')) # windows 默认编码是gbk linux 默认编码是 utf-8
phone1.close()