python socketserver实现服务器端执行命令 上传文件 断点续传

  1 基于socketserver在服务器端实现执行cmd命令和上传文件(断点续传)
  2 
  3 #server:
  4 
  5 #!/usr/bin/env python
  6 # -*- coding:utf-8 -*-
  7 import subprocess
  8 import socketserver
  9 import os
 10 
 11 BASEDIR = os.path.dirname((os.path.abspath(__file__)))
 12 
 13 class Myserver(socketserver.BaseRequestHandler):
 14 
 15     def handle(self):
 16         while True:
 17             conn = self.request
 18             conn.sendall(bytes('连接成功', encoding='utf8'))
 19             while True:
 20                 cmd_or_load = str(conn.recv(1024), encoding='utf8')
 21                 if not cmd_or_load:break
 22                 if cmd_or_load[0:3] == 'cmd':
 23                     cmd_name, cmd_str = cmd_or_load.split('|')
 24                     result = subprocess.getoutput(cmd_str)
 25                     result_lenth = len(result)
 26                     conn.sendall(bytes(str(result_lenth), encoding='utf8'))
 27                     conn.recv(1024) #解决粘包
 28                     conn.sendall(bytes(result, encoding='utf8'))
 29                 elif cmd_or_load[0:4] == 'load':
 30                     load_name, file_name, s_path, file_byte_size = cmd_or_load.split('|')
 31                     file_bytes_size = int(file_byte_size)
 32                     # file_path = os.path.join(BASEDIR, s_path, file_name)
 33                     file_path = BASEDIR+s_path+'\\'+file_name
 34 
 35                     has_rec = 0
 36 
 37                     if os.path.exists(file_path): #2001文件存在 2011 继续上传 2012重新上传
 38                         conn.sendall(bytes('2001', encoding='utf8'))
 39                         if str(conn.recv(1024), encoding='utf8') == '2011':
 40                             has_file_size = os.stat(file_path).st_size
 41                             has_rec += has_file_size
 42                             conn.sendall(bytes(str(has_file_size), encoding='utf8'))
 43                             f = open(file_path, 'ab')
 44                         elif str(conn.recv(1024)) == '2012':
 45                             f = open(file_path, 'wb')
 46                     else:
 47                         conn.sendall(bytes('2002', encoding='utf8'))
 48                         f = open(file_path, 'wb')
 49 
 50                     data_info = bytes()
 51                     while has_rec<file_bytes_size:
 52                         try:
 53                             data = conn.recv(1024)
 54                             if not data:
 55                                 raise Exception
 56                         except Exception:
 57                             break
 58                         has_rec += len(data)
 59                         f.write(data)
 60                     f.close()
 61                     print('写入成功')
 62 
 63 
 64 if __name__ == '__main__':
 65     server = socketserver.ThreadingTCPServer(('127.0.0.1', 8009), Myserver)
 66     server.serve_forever()
67 #---------------------------------------------------------------- 68 #clinet: 69 70 #!/usr/bin/env python 71 # -*- coding:utf-8 -*- 72 import socket 73 import os 74 75 76 sk = socket.socket() 77 sk.connect(('127.0.0.1', 8009)) 78 79 rec_str = str(sk.recv(1024), encoding='utf8') 80 print(rec_str) 81 82 83 def cmd(): 84 inp = input('请输入命令>>>') 85 comm = 'cmd|' 86 inp_bytes = bytes(comm + inp, encoding='utf8') 87 sk.sendall(inp_bytes) 88 cmd_lenth = int(str(sk.recv(1024), encoding='utf8')) 89 sk.sendall(bytes('1111', encoding='utf8')) # 解决粘包 90 has_recv = 0 91 cmd_info = bytes() 92 while has_recv<cmd_lenth: 93 fetch_info = sk.recv(1024) 94 has_recv += 1024 95 cmd_info += fetch_info 96 cmd_info_str = str(cmd_info ,encoding='utf8') 97 print(cmd_info_str) 98 99 def load(): 100 inp = input('请输入上传文件文件名 服务器保存路径>>>')#input:1.zip|\home 101 loadd = 'load|' 102 file_name = inp.split('|', 1)[0] 103 file_byte_size = os.stat(file_name).st_size 104 file_bytes_size = str(file_byte_size) 105 sk.sendall(bytes(loadd + inp + '|' + file_bytes_size, encoding='utf8')) 106 107 is_or_not = str(sk.recv(1024), encoding='utf8') 108 has_sent = 0 109 110 if is_or_not == '2001': 111 inp = input('文件已存在\n1、继续上传 2、重新上传>>>') 112 if inp == '1': 113 sk.sendall(bytes('2011', encoding='utf8')) 114 has_sent_size = int(str(sk.recv(1024), encoding='utf8')) 115 if has_sent_size == file_byte_size: 116 print('文件已全部发送') 117 else: 118 print('已经发送%d字节'%has_sent_size) 119 has_sent += has_sent_size 120 elif inp == '2': #2001文件存在 2011 继续上传 2012 重新上传 121 sk.sendall(bytes('2012', encoding='utf8')) 122 elif is_or_not == '2002': 123 pass 124 125 f = open(file_name, 'rb') 126 f.seek(has_sent) 127 while has_sent<file_byte_size: 128 data = f.read(1024) 129 sk.sendall(data) 130 has_sent += len(data) 131 print('\r已发送{}字节 共{}字节'.format(has_sent, file_byte_size), end='') 132 f.close() 133 print('\n发送成功') 134 135 136 while True: 137 inp = input('1、执行命令 2、上传文件>>>') 138 if inp == '1': 139 cmd() 140 elif inp == '2': 141 load()

 

转载于:https://www.cnblogs.com/minchen/p/8003755.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值