-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
185 lines (160 loc) · 7.31 KB
/
server.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
184
import socket
import threading
import os
import time
import psutil
import shutil
class server:
def __init__(self):
self.commandSock = socket.socket()
self.commandPort = 8080
self.transferSock = socket.socket()
self.transferPort = 8088
self.chatSock=socket.socket()
self.chatPort=8085
self.host = ''
self.bindsocket()
def bindsocket(self):
self.commandSock.bind((self.host, self.commandPort))
self.transferSock.bind((self.host, self.transferPort))
self.chatSock.bind((self.host,self.chatPort))
self.commandSock.listen(10)
self.transferSock.listen(10)
self.chatSock.listen(10)
self.filename = ""
print ("Waiting for a connection.....")
self.clientTransferSock, self.transferAddr = self.transferSock.accept()
self.clientCommandSock, self.commandAddr = self.commandSock.accept()
self.clientChatSock , self.chatAddr = self.chatSock.accept()
print("Got a transfer connection from %s" % str(self.transferAddr))
print("Got a command connection from %s" % str(self.commandAddr))
print("Got a chat connection from %s" % str(self.chatAddr))
self.sendPartitions()
self.clientCommandSock.send(('Partitions Sent').encode('utf-8'))
print('Partitions Sent!')
def closeServer(self):
self.clientCommandSock.close()
self.clientTransferSock.close()
self.clientChatSock.close()
def dicision(self):
while True:
self.message = (self.clientCommandSock.recv(32)).decode('utf-8')
#(self.message)
if self.message == 'Delete Request':
self.clientCommandSock.send('Delete Request Received'.encode('utf-8'))
self.delete()
elif self.message == 'Copy Request':
self.clientCommandSock.send('Copy Request Received'.encode('utf-8'))
self.copy()
elif self.message == 'Send File Request':
self.clientCommandSock.send('Send File Request Received'.encode('utf-8'))
self.sendFile()
elif self.message == 'Listdir Request':
self.clientCommandSock.send('Listdir Request Received'.encode('utf-8'))
self.listdir()
elif self.message == 'Chat Request':
self.clientCommandSock.send('Chat Request Received'.encode('utf-8'))
self.chat()
elif self.message == 'Mkdir Request':
self.clientCommandSock.send('Mkdir Request Received'.encode('utf-8'))
self.mkdir()
def chat(self):
self.chatfile=open('chatfile.txt','w')
self.message = self.clientChatSock.recv(128).decode('utf-8')
self.chatfile.write(self.message+'\n')
self.chatfile.close()
print(self.message)
def mkdir(self):
self.mkdirPath = self.clientCommandSock.recv(128).decode('utf-8')
try:
os.mkdir(self.mkdirPath)
self.clientCommandSock.send('Directory Made'.encode('utf-8'))
print ('Directory Made Successfully!')
except:
self.clientCommandSock.send('Directory Already Exist'.encode('utf-8'))
print ('Directory Already Exist')
def send(self, directory):
print(directory)
self.filename = directory.split('\\')[len(directory.split('\\')) - 1]
self.filename = self.filename.encode('utf-8')
self.nameSize = len(self.filename)
self.nameSize = str(self.nameSize).encode('utf-8')
self.clientTransferSock.send(self.nameSize)
while (self.clientTransferSock.recv(32)).decode('utf-8') != 'Name Size Received':
print('Waiting for Name Size to deliver...')
time.sleep(1)
else:
print('Name Size Delivered!')
self.clientTransferSock.send(self.filename)
while (self.clientTransferSock.recv(32)).decode('utf-8') != 'File Name Received':
print('Waiting for File Name to deliver...')
time.sleep(1)
else:
print('File Name Delivered!')
self.filename = self.filename.decode('utf-8')
# filename = os.path.join(path,filename)
self.fileSize = os.path.getsize(directory)
self.fileSize = str(self.fileSize).encode('utf-8')
self.clientTransferSock.send(self.fileSize)
while (self.clientTransferSock.recv(32)).decode('utf-8') != 'File Size Received':
print('Waiting for File Size to deliver...')
time.sleep(1)
else:
print('File Size Delivered!')
file_to_send = open(directory, 'rb')
lines = file_to_send.read()
self.clientTransferSock.sendall(lines)
file_to_send.close()
while (self.clientTransferSock.recv(32)).decode('utf-8') != 'File Received':
print('Waiting for File to deliver...')
time.sleep(1)
else:
print('File Delivered Successfully!')
def delete(self):
self.deleteDirectory = self.clientCommandSock.recv(128).decode('utf-8')
try:
os.remove(self.deleteDirectory)
self.clientCommandSock.send('File Deleted'.encode('utf-8'))
print ('Delete successfully!')
except:
self.clientCommandSock.send('File Not Found'.encode('utf-8'))
print ('File not found!')
def copy(self):
self.pathes = (self.clientCommandSock.recv(128).decode('utf-8')).split(',')
print(self.pathes)
#shutil.copy2(self.pathes[0], self.pathes[1])
try:
shutil.copy2(self.pathes[0], self.pathes[1])
self.clientCommandSock.send('File Copied'.encode('utf-8'))
print ('Copied successfully!')
except:
self.clientCommandSock.send('File Not Found or Access Denied'.encode('utf-8'))
print ('File Not Found or Access Denied')
def sendFile(self):
self.sendFileDirectory = self.clientCommandSock.recv(128).decode('utf-8')
self.clientCommandSock.send('File Directory Received'.encode('utf-8'))
threading.Thread(target=self.send, args=(self.sendFileDirectory,)).start()
def sendPartitions(self):
self.dps_defualt = psutil.disk_partitions()
fmt_str = "{:<8}"
fmt_str.format("Opts")
self.dps = [chr(x) + ":" for x in range(65, 90) if os.path.exists(chr(x) + ":")]
self.clientCommandSock.send((str(self.dps)).encode('utf-8'))
def listdir(self):
self.listdirPath = self.clientCommandSock.recv(128).decode('utf-8')
self.clientCommandSock.send('Listdir Path Received'.encode('utf-8'))
self.clientCommandSock.send(str(len(str(os.listdir(self.listdirPath)))).encode('utf-8'))
while (self.clientCommandSock.recv(32)).decode('utf-8') != 'Listdir Size Received':
print('Waiting for Listdir Size to deliver...')
time.sleep(1)
else:
print('Listdir Size Delivered!')
self.clientCommandSock.sendall(str(os.listdir(self.listdirPath)).encode('utf-8'))
while (self.clientCommandSock.recv(32)).decode('utf-8') != 'Listdir Received':
print('Waiting for Listdir to deliver...')
time.sleep(1)
else:
print('Listdir Delivered!')
if __name__ == '__main__':
myServer = server()
threading.Thread(target=myServer.dicision()).start()