Last active
May 18, 2019 01:22
-
-
Save jpignata/366582c0c89c28cefd8b0646d4842188 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import threading | |
import socket | |
CHARS = [chr(i).encode('ascii') for i in range(32, 127)] | |
NEWLINE = '\n'.encode('ascii') | |
def server(host='127.0.0.1', port=19): | |
def generate(client): | |
line = 1 | |
while True: | |
try: | |
for i in range(72): | |
char = CHARS[(line + i) % len(CHARS)] | |
client.send(char) | |
client.send(NEWLINE) | |
except Exception: | |
client.close() | |
break | |
line += 1 | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
sock.bind((host, port)) | |
sock.listen() | |
while True: | |
client, _ = sock.accept() | |
thread = threading.Thread(target=generate, args=(client,)) | |
thread.start() | |
server() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment