Skip to content

Instantly share code, notes, and snippets.

@meatyite
Last active May 11, 2019 12:10
Show Gist options
  • Save meatyite/c2f11454bfc1cbd40e07c4ef1fc657c7 to your computer and use it in GitHub Desktop.
Save meatyite/c2f11454bfc1cbd40e07c4ef1fc657c7 to your computer and use it in GitHub Desktop.
Python Typewriter effect in Telnet
#!/usr/bin/python3
#########################################
# Made by sl4v #
# Licensed under the WTFPL, #
# see http://www.wtfpl.net/txt/copying/ #
#########################################
import sys
import socket
from time import sleep
from threading import Thread
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
filenames = []
# if you doing this locally, then maybe put a lower number in here
wait_before_each_char = 0.1
host = ''
port = 42069
max_users_connected = 10
def serverMain():
print("[INFO] Binding to *:" + str(port))
try:
s.bind((host, port))
except Exception as e:
print("[ERROR] Failed to bind: " + str(e))
try:
s.listen(max_users_connected)
print("[INFO] Server listening.")
except Exception as e:
print("[ERROR] Failed to listen: " + str(e))
while True:
conn, addr = s.accept()
print("[INFO] CLIENT CONNECTED FROM " + addr[0] + ":" + str(addr[1]))
Thread(target=clientThread, args=(conn,)).start()
def genLoginMsg():
login_msg = 'pyTypingCinemaTelnet V2\r\n\r\n'
filename_num = 0
for filename in filenames:
login_msg += "[" + str(filename_num) + "] - " + \
filenames[filename_num] + '\r\n'
filename_num += 1
login_msg += "\r\nEnter the number of the text file to read from:"
return login_msg
def clientThread(client):
client.send(genLoginMsg().encode())
filename_num = client.recv(1024).decode().replace(
'\r\n', '\r').replace('\r', '').replace('\n', '')
filename = ''
try:
filename_num = int(filename_num)
filename = filenames[filename_num]
except ValueError:
client.send('Not a valid number, Goodbye.'.encode())
return 0
except IndexError:
client.send('Not a valid number, Goodbye.'.encode())
return 0
# if anything besides CRLF is used, replace it with CRLF.
file_chars = open(filename, 'r').read().replace(
'\r', '\r\n').replace('\r\n', '\n').replace('\n', '\r\n')
try:
client.send('\r\n'.encode())
for file_char in file_chars:
client.sendall(file_char.encode())
sleep(wait_before_each_char)
return 0 # if a value is returned, the function quits, and thats what we want to do: disconnect the client by exiting this function
except Exception as e:
print(str(e))
if __name__ == "__main__":
for arg_num in range(1, len(sys.argv)):
arg = sys.argv[arg_num]
if arg == '-w':
wait_before_each_char = float(sys.argv[arg_num + 1])
elif arg == '-f':
filenames.append(sys.argv[arg_num + 1])
elif arg == '-m':
max_users_connected = int(sys.argv[arg_num + 1])
elif arg == '-p':
port = int(sys.argv[arg_num + 1])
elif arg == '-h' or arg == '--help':
print("""\
usage: pyTypingCinemaTelnet [flag1] [flag1 arg]
-h show this help menu and quit
-w [time] how much time (in seconds) to wait before each char, decimals are also allowed
-f [file] file to read from (can be used multiple times)
-m [users] max users that can connect to the server
-p [port] port to listen on
""")
sys.exit(0)
serverMain()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment