1#!/usr/bin/env python3
2
3import socket
4
5HOST = '127.0.0.1' # Standard loopback interface address (localhost)
6PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
7
8with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 s.bind((HOST, PORT))
10 s.listen()
11 conn, addr = s.accept()
12 with conn:
13 print('Connected by', addr)
14 while True:
15 data = conn.recv(1024)
16 if not data:
17 break
18 conn.sendall(data)
19
1#!/usr/bin/env python3
2
3import socket
4
5HOST = '127.0.0.1' # The server's hostname or IP address
6PORT = 65432 # The port used by the server
7
8with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 s.connect((HOST, PORT))
10 s.sendall(b'Hello, world')
11 data = s.recv(1024)
12
13print('Received', repr(data))
14
1conn, addr = s.accept()
2with conn:
3 print('Connected by', addr)
4 while True:
5 data = conn.recv(1024)
6 if not data:
7 break
8 conn.sendall(data)
9
1with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
2 pass # Use the socket object without calling s.close().
3
1HOST = '127.0.0.1' # Standard loopback interface address (localhost)
2PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
3
4# ...
5
6s.bind((HOST, PORT))
7