Socket code in levels of complexity

From MyWiki
Revision as of 10:13, 20 January 2022 by George2 (Talk | contribs)

Jump to: navigation, search

Complexity 1, very simple, connect using telnet localhost 9988, send a few characters and then press <enter>
The characters are echoed back and the connection is disconnected

#!/usr/bin/python3
 
import socket
import sys
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", 9988))
s.listen(1)
 
while True:
    conn, addr = s.accept()
    data = conn.recv(1024)
    conn.sendall(data)
    conn.close()