-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho-server.py
43 lines (37 loc) · 1.61 KB
/
echo-server.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
40
41
42
# =====================================================
# Purpose:
# Illustrate the Socket > Bind > Listen > Accept
# > Receive > Send > Close paradigm illustrated in
# 2_tcpipmodel.md
# socket.socket(IPv4, TCP)
# socket.bind((Host, Port))
# socket.listen() # ready to accept connections
# socket.accept() # blocks port & return a new socket
# # object representing the connection
# conn.recv
# conn.send()
# =====================================================
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 44444 # Port to listen on. Non-privileged ports are > 1023
# Argument 1: address family (AF_INET = IPv4)
# Argument 2: socket type (SOCK_STREAM = TCP, SOCK_DGRAM = UDP)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
# accept() blocks and waits for an incoming connection
# when a client connects, it returns a new socket object representing the connection
# and a tuple holding the address of the client. (host, port) for IPv4 connections
# this new socket is what we use to communicate with the client; it's different from
# the listening socket that the server is using to accept new connections
conn, addr = s.accept()
# loop over blocking calls
with conn:
print('Connected by', addr)
print('Server listening on port', PORT)
while True:
data = conn.recv(1024)
if not data:
break
# reads whatever data the client sends and echoes back using .sendall()
conn.sendall(data)