Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
PyTchat/client.py

61 lines
1.5 KiB
Python
Raw Permalink Normal View History

2019-07-15 22:42:53 +02:00
from threading import Thread
from SecureSocketService import Socket
from sys import exit
from socket import error as socket_error
2019-07-15 22:42:53 +02:00
class Client(Socket):
def __init__(self, host: str, port: int, service_id: int = 2):
super().__init__()
self.service_id = service_id
print("Connecting...")
self.connect_server(host, port)
def receive_server(self):
try:
data = self.receive(self.socket)
except socket_error:
self.quit()
return False
else:
if data.lower() == "quit":
2019-07-15 22:42:53 +02:00
self.quit()
return False
return data
2019-07-15 22:42:53 +02:00
def send_server(self, data):
try:
self.send(self.socket, data)
except socket_error:
self.quit()
def send_input(self):
2019-07-15 22:42:53 +02:00
while True:
data = input()
self.send_server(data)
2019-07-15 22:42:53 +02:00
if data.lower() == "quit":
break
def receive_print(self):
while True:
message = self.receive_server()
if message:
print(message)
else:
break
2019-07-15 22:42:53 +02:00
def quit(self):
try:
self.socket.close()
except socket_error:
pass
finally:
print("Disconnected")
exit()
2019-07-15 22:42:53 +02:00
if __name__ == "__main__":
client = Client("localhost", 3621)
Thread(target=client.receive_print).start()
Thread(target=client.send_input).start()