-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
21 lines (15 loc) · 888 Bytes
/
Copy pathserver.py
File metadata and controls
21 lines (15 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import socket #Import the socket to use it
s = socket.socket() #Creation of the socket this takes ipv4 by default & tcp as protocol by default
print("Socket created")
#Since we're creating a server it should listens for connections to bind to
s.bind(('localhost', 9999)) #It accepts ip address and port range 0 -> 65535 as one object
s.listen(3) #It takes no of clients for connection
print('Waiting for connections')
#Once we're listening we are waiting for connections to run in continous manner
while True:
c, address = s.accept() #This returns client's socket and address & is responsible for accepting connections
name = c.recv(1024).decode()
print(f"Connected with {name}, at {address}.")
#Sending something to client side
c.send(bytes("Welcome at localhost", 'utf-8')) #It takes a bytes like object in a format
c.close() #Always close the resources