chatServer/serverBlaster.py

68 lines
1.4 KiB
Python
Raw Normal View History

2021-04-09 13:16:29 +00:00
#!/usr/bin/env python3
import sys
oldexcepthook = sys.excepthook
def newexcepthook(type,value,traceback):
oldexcepthook(type,value,traceback)
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)
# script start
import socket
2021-04-09 21:46:48 +00:00
import threading
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")
print("server: " +response)
2021-04-09 13:16:29 +00:00
def sendRequest(connection,data):
connection.sendall(len(data).to_bytes(4,"big") + data)
def getResponse(connection):
data = b''
data = connection.recv(4)
if not data:
connection.close()
return
requestLength = int.from_bytes(data,"big")
data = connection.recv(requestLength)
return data
def main():
global connection
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2021-05-23 11:04:49 +00:00
connection.connect((sys.argv[1],int(sys.argv[2])))
2021-04-09 21:46:48 +00:00
thread = receiverThread(connection)
thread.start()
2021-04-09 13:16:29 +00:00
while True:
2021-04-09 21:46:48 +00:00
text = input()
2021-04-09 13:16:29 +00:00
data = text.encode("utf-8")
2021-04-09 21:46:48 +00:00
connection.settimeout(15)
sendRequest(connection,data)
connection.settimeout(None)
2021-04-09 13:16:29 +00:00
if text == "exit":
connection.close()
break
if __name__ == '__main__':
main()