chatServer/dumbclient.py

93 lines
2.0 KiB
Python
Raw Permalink Normal View History

2021-10-28 06:08:33 +00:00
#!/usr/bin/env python3
import sys
stopOnException = False
oldexcepthook = sys.excepthook
def newexcepthook(type,value,traceback):
oldexcepthook(type,value,traceback)
if stopOnException: input("Press ENTER to quit.")
sys.excepthook = newexcepthook
import os
p = os.path.join
pUp = os.path.dirname
s = False
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
s = os.path.realpath(sys.executable)
else:
s = os.path.realpath(__file__)
sp = pUp(s)
import socket
import threading
import queue
import dumbconsole
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.settimeout(600)
connection.connect(("127.0.0.1",1337))
class receiverThread(threading.Thread):
def __init__(self,connection):
threading.Thread.__init__(self)
self.connection = connection
def run(self):
while True:
response = getResponse(connection).decode("utf-8")
dumbconsole.inputQueue.put(response)
def getResponse(connection):
data = b''
data = connection.recv(4)
if not data: return False
nul = connection.recv(1)
if not nul: return False
if nul != b"\x00": return False
requestLength = int.from_bytes(data,"big")
return connection.recv(requestLength)
def sendResponse(connection,data):
connection.sendall(len(data).to_bytes(4,"big") + b"\x00" + data)
def commandlineToList(cmd):
args = []
cArg = ""
escape = False
quoted = False
for letter in cmd:
if escape == True:
cArg += letter
escape = False
continue
if letter == "\\":
escape = True
continue
if letter == ",":
if cArg == "": continue
args.append(cArg)
cArg = ""
continue
cArg += letter
args.append(cArg)
return args
def listToCommandline(lst):
cmd = ""
for arg in lst:
arg = arg.replace("\\","\\\\")
arg = arg.replace(",","\\,")
cmd += arg + ","
return cmd[:-1]
def dumbSend(text):
sendResponse(connection,text.encode("utf-8"))
thread = receiverThread(connection)
thread.start()
dumbconsole.init(dumbSend)