python socketserver实现服务器端执行命令 上传文件 断点续传
生活随笔
收集整理的這篇文章主要介紹了
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()
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
總結
以上是生活随笔為你收集整理的python socketserver实现服务器端执行命令 上传文件 断点续传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python3 实现用户登陆,输入三次密
- 下一篇: FNAFWORLD里面的锁匙孔障碍可以绕