forked from sharunrajeev/YourFirstContribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoddEvenServer.py
39 lines (35 loc) · 1.19 KB
/
oddEvenServer.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
import socket
# IP Address of my system
IP = "192.168.10.127"
print(IP)
PORT = 4567
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 file data from the client. """
data = conn.recv(SIZE).decode(FORMAT)
print(f"[RECV] Receiving the data from server")
# Logic
if int(data)%2 == 0:
print(f"[CLIENT]: {data}")
conn.send("Even Number".encode(FORMAT))
else:
conn.send("Odd Number".encode(FORMAT))
""" Closing the connection from the client. """
conn.close()
print(f"[DISCONNECTED] {addr} disconnected.")
if __name__ == "__main__":
main()