forked from dxghost/JMFileManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
185 lines (162 loc) · 7.47 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import socket
import os
import time
import threading
import psutil
# import thread
class client:
def __init__(self, commandPort, transferPort, host):
self.commandSock = socket.socket()
self.commandPort = commandPort
self.transferSock = socket.socket()
self.transferPort = transferPort
self.chatSock=socket.socket()
self.chatPort = 8085
self.host = host
def connectToServer(self):
self.commandSock.connect((self.host, self.commandPort))
self.transferSock.connect((self.host, self.transferPort))
self.chatSock.connect((self.host,self.chatPort))
self.dps = eval(self.commandSock.recv(2 ** 10).decode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'Partitions Sent':
print('Waiting for Partitions to receive...')
time.sleep(1)
else:
print('Partitions Received!')
print(self.dps)
return self.dps
def chat(self,message):
self.commandSock.send(('Chat Request').encode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'Chat Request Received':
print('Waiting for Chat Request to deliver...')
time.sleep(1)
else:
print('Chat Request Delivered!')
self.chatSock.send((message).encode('utf-8'))
def recive(self, seveDirectory=''):
print('Receiving...')
self.size = self.transferSock.recv(128)
self.transferSock.send('Name Size Received'.encode('utf-8'))
# if not self.size:
# break
self.size = int(self.size.decode('utf-8'))
self.filename = self.transferSock.recv(self.size)
self.transferSock.send('File Name Received'.encode('utf-8'))
self.filesize = self.transferSock.recv(64)
self.transferSock.send('File Size Received'.encode('utf-8'))
self.filesize = int(self.filesize.decode('utf-8'))
print(self.filename)
self.file_to_write = open(seveDirectory + self.filename.decode('utf-8'), 'wb')
self.chunksize = 4096
def fileReceiving():
while self.filesize > 0:
if self.filesize < self.chunksize:
self.chunksize = self.filesize
self.data = self.transferSock.recv(self.chunksize)
self.file_to_write.write(self.data)
self.filesize -= len(self.data)
else:
self.file_to_write.close()
print('File received successfully!')
self.transferSock.send('File Received'.encode('utf-8'))
threading.Thread(target=fileReceiving).start()
def deleteRequest(self, fileDirectory):
self.commandSock.send(('Delete Request').encode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'Delete Request Received':
print('Waiting for Delete Request to deliver...')
time.sleep(1)
else:
print('Delete Request Delivered!')
self.commandSock.send((fileDirectory).encode('utf-8'))
self.deleteStatus = (self.commandSock.recv(32)).decode('utf-8')
while (self.deleteStatus != 'File Deleted' and self.deleteStatus != 'File Not Found'):
print('Waiting for Delete Status...')
time.sleep(1)
else:
if self.deleteStatus == 'File Deleted':
print('File Deleted Successfully!')
elif self.deleteStatus == 'File Not Found':
print('File Not Found!')
def copyRequest(self, fileDirectory, distanationPath):
self.commandSock.send(('Copy Request').encode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'Copy Request Received':
print('Waiting for Copy Request to deliver...')
time.sleep(1)
else:
print('Copy Request Delivered!')
self.commandSock.send((fileDirectory + ',' + distanationPath).encode('utf-8'))
self.deleteStatus = (self.commandSock.recv(32)).decode('utf-8')
while (self.deleteStatus != 'File Copied' and self.deleteStatus != 'File Not Found or Access Denied'):
print('Waiting for Copy Status...')
time.sleep(1)
else:
if self.deleteStatus == 'File Copied':
print('File Copied Successfully!')
elif self.deleteStatus == 'File Not Found or Access Denied':
print('File Not Found or Access Denied!')
def sendFileRequest(self, fileDirectory):
self.commandSock.send(('Send File Request').encode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'Send File Request Received':
print('Waiting for Send File Request to deliver...')
time.sleep(1)
else:
print('Send File Request Delivered!')
self.commandSock.send((fileDirectory).encode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'File Directory Received':
print('Waiting for File Directory to deliver...')
time.sleep(1)
else:
print('File Directory Delivered!')
threading.Thread(target=self.recive()).start()
def mkdirRequest(self, path):
self.commandSock.send(('Mkdir Request').encode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'Mkdir Request Received':
print('Waiting for Mkdir Request to deliver...')
time.sleep(1)
else:
print('Mkdir Request Delivered!')
self.commandSock.send((path).encode('utf-8'))
self.mkdirStatus = (self.commandSock.recv(32)).decode('utf-8')
while (self.mkdirStatus != 'Directory Made' and self.mkdirStatus != 'Directory Already Exist'):
print('Waiting for Making Status...')
time.sleep(1)
else:
if self.mkdirStatus == 'Directory Made':
print('Directory Made Successfully!')
elif self.mkdirStatus == 'Directory Already Exist':
print('Directory Already Exist!')
def listdirRequest(self, path):
self.commandSock.send(('Listdir Request').encode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'Listdir Request Received':
print('Waiting for Listdir Request to deliver...')
time.sleep(1)
else:
print('Listdir Request Delivered!')
self.commandSock.send(path.encode('utf-8'))
while (self.commandSock.recv(32)).decode('utf-8') != 'Listdir Path Received':
print('Waiting for Listdir Path to deliver...')
time.sleep(1)
else:
print('Listdir Path Delivered!')
self.listdirSize = int((self.commandSock.recv(128)).decode('utf-8'))
self.commandSock.send(('Listdir Size Received').encode('utf-8'))
self.listdir = ''
self.chunksize = 4096
while self.listdirSize > 0:
if self.listdirSize < self.chunksize:
self.chunksize = self.listdirSize
self.data = self.commandSock.recv(self.chunksize).decode('utf-8')
self.listdir += self.data
self.listdirSize -= len(self.data)
else:
self.listdir = eval(self.listdir)
print('Listdir received successfully!')
self.commandSock.send('Listdir Received'.encode('utf-8'))
print(self.listdir)
return self.listdir
def closeSocket(self):
self.commandSock.close()
self.transferSock.close()
if __name__ == '__main__':
myClient = client(8080, 8088,'localhost')
myClient.connectToServer()