-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
161 lines (111 loc) · 4.79 KB
/
Copy pathserver.py
File metadata and controls
161 lines (111 loc) · 4.79 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
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
from os import path
from Loggers.ConsoleLogger import ConsoleLogger
from Loggers.FileLogger import FileLogger
import struct
import time
import sys
import threading
import socket
import glob
from inspect import currentframe, getframeinfo
HEADER = 1024
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = "utf-8"
DISCONNECT_MESSAGE = "!DISCONNECT"
CHANGE_NAME_MESSAGE = "!NAME="
FILE_NAME_MESSAGE = "!FILE="
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
clients = {}
logger = FileLogger(path.basename(__file__))
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
sender = addr
connected = True
while connected:
# wait until you recivce info from the client
try:
msg_length = conn.recv(HEADER).decode(FORMAT)
# data = conn.recv(HEADER).decode(FORMAT)
if msg_length: # we recieve nothing when we connect the first time
if(msg_length == "!U_FILE="):
uploadFile(conn,sender)
continue
elif(msg_length == "!D_FILE="):
sendFile(conn)
continue
elif(msg_length == "!LISTF"):
listFiles(conn)
continue
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if CHANGE_NAME_MESSAGE in msg:
tempName = msg.split(CHANGE_NAME_MESSAGE)[1]
print(f"[{sender}] changed their name to {tempName}")
sendToAllClients(str.encode(
f"[{sender}] changed their name to {tempName}\n"))
sender = tempName
continue
if(msg == DISCONNECT_MESSAGE):
print(f"[{sender}]: DISCONNECTED")
sendToAllClients(str.encode(f"[{sender}]: DISCONNECTED\n"))
connected = False
break
print(f"[{sender}]: {msg}")
sendToAllClients(str.encode(f"[{sender}]: {msg}\n"))
except Exception as e:
logger.LogToFile(e, getframeinfo(currentframe()).lineno)
sys.exit()
del clients[addr]
conn.close()
def uploadFile(conn,sender):
msg_length = conn.recv(HEADER).decode(FORMAT)
msg_length = int(msg_length)
fileName = conn.recv(msg_length).decode(FORMAT)
sendToAllClients(str.encode(f"[{sender}]: Uploaded U_{fileName} \n"))
with open("UploadedFiles/U_"+fileName, "w+") as fileRecived:
msg_length = conn.recv(HEADER).decode(FORMAT)
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
fileRecived.write(msg)
def sendFile(conn):
msg_length = conn.recv(HEADER).decode(FORMAT)
msg_length = int(msg_length)
fileName = conn.recv(msg_length).decode(FORMAT)
try:
# Check if the file exists
content = open(f"UploadedFiles/{fileName}", "r")
logger.LogToFile("FILE FOUND", getframeinfo(currentframe()).lineno)
logger.LogToFile(f"Sendings the contents of {fileName}", getframeinfo(currentframe()).lineno)
conn.send(content.read().encode(FORMAT))
except:
logger.LogToFile(f"[{fileName}] File not found.",getframeinfo(currentframe()).lineno)
conn.send("404: The file you requested does not exist".encode(FORMAT))
finally:
content.close()
def listFiles(conn):
path = 'UploadedFiles'
files = [f for f in glob.glob(path + "**/*.*", recursive=True)]
for file in files:
conn.send(("- "+file.split("\\")[1]+"\n").encode(FORMAT))
def start():
server.listen() # listen for new connections
logger.LogToFile("Server is listening",
getframeinfo(currentframe()).lineno)
print(f"[LISTENING] Server is listening on {SERVER}")
while True:
# we will store the address of the new connection and store a sokcet object that will allow us to send info back to that connection
conn, addr = server.accept()
clients[addr] = conn
# we will pass the conenction to handle client and then start the thread
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
# tells us how many threads are connected
print(f"[ACTIVE CONNECTIONS] {threading.activeCount()-1}")
def sendToAllClients(message):
for client in clients:
clients[client].send(message)
print("[STARTING] Server is starting... ")
start()