-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
28 lines (22 loc) · 1.09 KB
/
Copy pathserver.py
File metadata and controls
28 lines (22 loc) · 1.09 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
# this is server side socket programming to establish an incoming connection from a client.
import socket
# we can also use "localhost" below.
HOST = '192.168.1.4'
# we don't use any of common port that are used by our system
PORT = 9090
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
# server.listen(n) specifies up to how many connection our server will handle before leaving out any new
# incoming request.
server.listen(5)
while True:
# server.accept() method is used to accept an incoming connection.
communication_socket, address = server.accept()
print(f"connected to {address}")
# communication_socket.recv(n) --> here n defines number of bytes our server receives from the clint in msg.
# this message is then decoded using utf-8, we can also choose ascii
message = communication_socket.recv(1024).decode('utf-8')
print(f"message received from client is : {message}")
communication_socket.send(f"received you message. Thank you!". encode('utf-8'))
communication_socket.close()
print(f"communication with {address} closed.")