Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Created October 26, 2016 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/1f3f5b606a19bec7defae9501fa398d5 to your computer and use it in GitHub Desktop.
Save QuantumFractal/1f3f5b606a19bec7defae9501fa398d5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
A simple echo server
"""
import socket
host = ''
port = 20602
backlog = 5
size = 20
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
anchor = None
while True:
client, address = s.accept()
print('Client connected ({})'.format(address))
data = client.recv(size)
if data:
print(data)
if anchor is not None:
client.send(b'yes')
data = client.recv(100)
if data:
client.send((anchor[0]+','+str(anchor[1])).encode())
client.send(anchor[2])
else:
client.send(b'no')
data = client.recv(100)
if data:
guid, size = data.decode().split(',')
print("GUID {}".format(guid))
size = int(size)
client.send(b'bird up')
data = client.recv(size)
if data:
anchor_data = bytes(data)
client.send(b'best show on tv')
anchor = (guid, size, anchor_data)
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment