-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_txt_file.py
More file actions
48 lines (36 loc) · 1.35 KB
/
server_txt_file.py
File metadata and controls
48 lines (36 loc) · 1.35 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
import socket
SERVER = "192.168.178.139"
PORT = 5050
ADDR = (SERVER, PORT)
FORMAT = "utf-8"
SIZE = 1024
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 content:' {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()