700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python socketserver实现服务器端执行命令 上传文件 断点续传

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

时间:2022-02-25 06:39:26

相关推荐

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 15def 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文件存在 继续上传 重新上传 38conn.sendall(bytes('2001', encoding='utf8')) 39if str(conn.recv(1024), encoding='utf8') == '': 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') 44elif str(conn.recv(1024)) == '': 45 f = open(file_path, 'wb') 46 else: 47conn.sendall(bytes('2002', encoding='utf8')) 48f = open(file_path, 'wb') 49 50 data_info = bytes() 51 while has_rec<file_bytes_size: 52try: 53 data = conn.recv(1024) 54 if not data: 55 raise Exception 56except Exception: 57 break 58has_rec += len(data) 59f.write(data) 60 f.close() 61 print('写入成功') 62 63 64 if __name__ == '__main__': 65server = socketserver.ThreadingTCPServer(('127.0.0.1', 8009), Myserver) 66server.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(): 84inp = input('请输入命令>>>') 85comm = 'cmd|' 86inp_bytes = bytes(comm + inp, encoding='utf8') 87sk.sendall(inp_bytes) 88cmd_lenth = int(str(sk.recv(1024), encoding='utf8')) 89sk.sendall(bytes('1111', encoding='utf8')) # 解决粘包 90has_recv = 0 91cmd_info = bytes() 92while has_recv<cmd_lenth: 93 fetch_info = sk.recv(1024) 94 has_recv += 1024 95 cmd_info += fetch_info 96cmd_info_str = str(cmd_info ,encoding='utf8') 97print(cmd_info_str) 98 99 def load():100inp = input('请输入上传文件文件名 服务器保存路径>>>')#input:1.zip|\home101loadd = 'load|'102file_name = inp.split('|', 1)[0]103file_byte_size = os.stat(file_name).st_size104file_bytes_size = str(file_byte_size)105sk.sendall(bytes(loadd + inp + '|' + file_bytes_size, encoding='utf8'))106 107is_or_not = str(sk.recv(1024), encoding='utf8')108has_sent = 0109 110if is_or_not == '2001':111 inp = input('文件已存在\n1、继续上传 2、重新上传>>>')112 if inp == '1':113 sk.sendall(bytes('', 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_size120 elif inp == '2': #2001文件存在 继续上传 重新上传121 sk.sendall(bytes('', encoding='utf8'))122elif is_or_not == '2002':123 pass124 125f = open(file_name, 'rb')126f.seek(has_sent)127while 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='')132f.close()133print('\n发送成功')134 135 136 while True:137inp = input('1、执行命令 2、上传文件>>>')138if inp == '1':139 cmd()140elif inp == '2':141 load()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。