-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
44 lines (35 loc) · 1.65 KB
/
Copy pathserver.py
File metadata and controls
44 lines (35 loc) · 1.65 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
import socket
import threading
#treading allow to run multiple threads at a same time
PORT=5050
SERVER = socket.gethostbyname(socket.gethostname())
#geting the ip address of the system instead by simply specifying it
ADDR = (SERVER,PORT)
#need to be in tuple (wraping the 2 variable in one variable)
#now bingin the socket to that address
server = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
#af_alg specifing the family of socket that we want (inet is ipv4)
# sock_stream streaming the data through the socket
# socket.socket in creating new socket
server.bind(ADDR)
#meaning that we are bonding the socket to that address
def handle_client(conn, addr):#this will handle the connection between the client and the server
# this function will be running concerenttly for each client
# displaying who connected
print(f"[NEW CONNECTION ]:-{addr} is connected")
connected = True
while connected:
def start():
server.listen()
while True:
conn , addAF_ALGr = server.accept()
#on getting connection to the server what we are doing is
#we store the address of the connectio to addr and what port it came from
# conn to store the object to send informtaion back to the connection
tread = threading.Thread(target=handle_client , args=(conn , addr))#set target while making the thread ...and pass the arguement
tread.start()
# display active connection
print(f"[ACTIVE CONNECTION ] :- {threading.active_count() -1}")
# why -1 becoz the start() thread is always runing thats to match that we sub -1
print("[ STARTING ]...!!!!!!....starver is getting start")#\
start()