-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
46 lines (36 loc) · 1.36 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
import socket
IP = socket.gethostbyname(socket.gethostname())
PORT = 4455
ADDR = (IP, PORT)
SIZE = 1024
FORMAT = "utf-8"
def main():
print("[STARTING] Server is starting.")
""" Staring a TCP socket. """
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
""" Bind the IP and PORT to the server. """
server.bind(ADDR)
""" Server is listening, i.e., server is now waiting for the client to connected. """
server.listen()
print("[LISTENING] Server is listening.")
while True:
""" Server has accepted the connection from the client. """
conn, addr = server.accept()
print(f"[NEW CONNECTION] {addr} connected.")
""" Receiving the filename from the client. """
filename = conn.recv(SIZE).decode(FORMAT)
print(f"[RECV] Receiving the filename.")
file = open(filename, "w")
conn.send("Filename received.".encode(FORMAT))
""" Receiving the file data from the client. """
data = conn.recv(SIZE).decode(FORMAT)
print(f"[RECV] Receiving the file data.")
file.write(data)
conn.send("File data received".encode(FORMAT))
""" Closing the file. """
file.close()
""" Closing the connection from the client. """
conn.close()
print(f"[DISCONNECTED] {addr} disconnected.")
if __name__ == "__main__":
main()