Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
73 lines (45 loc) · 1.46 KB
/
Copy pathserver.py
File metadata and controls
73 lines (45 loc) · 1.46 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
import socket
import threading
#create socket,
#bind socket
#listen
#accept
#handle message
#close socket
# these are the processes one should undergo to successfully connent create the server and connect to the clients
HEADER = 64
FORMAT = 'utf-8'
PORT = 5050
close = "Disconnect"
SERVER = socket.gethostbyname(socket.gethostname())
hostname = socket.gethostname()
print(f'current server {hostname} running')
print(f"IP Address {SERVER} Running")
ADDR = (SERVER,PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#creation of new socket ,
server.bind(ADDR)
#binding the created socket to the ADDR, which is to the ip address and port of the server
def handle_client(conn,addr):
print(f"NEW client {addr} connected")
connection = True
while connection:
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length =int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == close:
print(f"the message is: {msg}")
connection = False
print(f"[{addr}] {msg}")
conn.close()
def start():
server.listen()
print(f"Listening {SERVER}")
while True:
conn,addr = server.accept();
thread = threading.Thread(target = handle_client, args=(conn,addr))
thread.start()
print(threading.activeCount()-1)
print('Starting: Server is starting ...')
start()