chatServer/serverBlaster.py
2021-04-09 15:16:29 +02:00

61 lines
1.3 KiB
Python

#!/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
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)
connection.connect(("127.0.0.1",21779))
while True:
text = input("data: ")
data = text.encode("utf-8")
sendRequest(connection,data)
response = getResponse(connection).decode("utf-8")
print("server: " +response)
if text == "exit":
connection.close()
break
if text == "close":
connection.close()
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("127.0.0.1",21779))
connection.close()
break
if __name__ == '__main__':
main()