Skip to content

Instantly share code, notes, and snippets.

@includeamin
Last active June 16, 2019 08:44
Show Gist options
  • Save includeamin/feac687a9342c8f4b15f1b357293646e to your computer and use it in GitHub Desktop.
Save includeamin/feac687a9342c8f4b15f1b357293646e to your computer and use it in GitHub Desktop.
Simple echo server python socket
# Echo client program
import socket
HOST = 'localhost'
PORT = 3003
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
# Echo server program
import socket
HOST = ''
PORT = 3003
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
data = str(data)[2:-1].upper()
conn.sendall(data.encode())
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment