-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
132 lines (111 loc) · 4.29 KB
/
server.py
File metadata and controls
132 lines (111 loc) · 4.29 KB
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
from socket import *
import select
import sys
import threading
import time
QUIT = False
class Server:
def __init__(self):
self.host = "10.220.82.40"
self.port = 5089
self.threads = []
self.backlog = 10
def run(self):
while True:
try:
all_good = False
self.sock = socket(AF_INET, SOCK_STREAM)
self.sock.bind((self.host, self.port))
self.sock.listen(self.backlog)
print("Server started at " + self.host + ":" + str(self.port))
break
except Exception as err:
print('Socket connection error... ')
self.sock.close()
try:
while True:
try:
client, addr = self.sock.accept()
except socket.timeout:
continue
new_thread = Client(client)
print("Connected by ", addr)
msg = ("User: %s connected from: %s" % (new_thread.name, addr)).encode()
for each in self.threads:
each.client.send(msg)
self.threads.append(new_thread)
new_thread.start()
for thread in self.threads:
if not thread.is_alive():
self.threads.remove(thread)
thread.join()
# time.sleep(1)
except KeyboardInterrupt:
print("Terminating by Ctrl+C")
except Exception as err:
print("Exception: %s\nClosing" % err)
for thread in self.threads:
thread.join()
self.sock.close()
class Client(threading.Thread):
def __init__(self, client):
threading.Thread.__init__(self)
self.client = client
time.sleep(1)
self.done = False
def run(self):
while not self.done:
try:
cmd = self.client.recv(1024).decode()
if cmd.startswith("/name"):
self.client.send("Enter your username: ".encode())
old_name = self.name
self.name = self.client.recv(1024).decode()
msg = "%s has changed his username to %s" % (old_name, self.name)
for each in server.threads:
if each != self and each.is_alive():
each.client.send(msg.encode())
self.client.send(("Your username has been changed to %s" % self.name).encode())
elif cmd == "/quit":
self.remove()
elif cmd.startswith("/filetransfer"):
self.client.send("Please enter the filename of the file: ".encode())
filename = self.client.recv(1024).decode()
file = open(filename, 'rb')
file_data = file.read(4096)
for each in server.threads:
if each != self and each.is_alive():
self.fileTransfer(each)
print("Data has been transmitted successfully")
else:
msg = "%s: %s" % (self.name, cmd)
for each in server.threads:
if each != self:
each.client.send(msg.encode())
except Exception as e:
print("Connection lost", e)
# self.remove()
break
# continue
server.threads.remove(self)
self.client.close()
return
def remove(self):
self.client.send("exit".encode())
server.threads.remove(self)
QUIT = True
self.done = True
def fileTransfer(self, user):
# filename = input(str("Please enter a filename for the incoming file: "))
user.client.send("Please enter a filename for the incoming file: ".encode())
filename = user.client.recv(1024).decode()
with open(filename, 'wb') as file:
file_data = user.client.recv(4096)
file.write(file_data)
# file = open(filename, 'wb')
# file.close()
print("File has been received successfully.")
if __name__ == "__main__":
server = Server()
server.run()
print("Terminated")