Created
October 26, 2016 03:11
-
-
Save QuantumFractal/1f3f5b606a19bec7defae9501fa398d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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