-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
29 lines (23 loc) · 779 Bytes
/
client.py
File metadata and controls
29 lines (23 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import time
import threading
import selectors
import socket
class ChatClient:
def __init__(self, host, port):
self._host = host
self._port = port
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._read_selector = selectors.DefaultSelector()
def _input_and_send_loop(self):
while True:
msg = input()
self._socket.send(msg.encode("utf8"))
def connect(self):
self._socket.connect((self._host, self._port))
threading.Thread(target=self._input_and_send_loop).start()
while True:
msg = self._socket.recv(1024).decode("utf8")
print(" < " + msg)
if __name__ == "__main__":
client = ChatClient("localhost", 7342)
client.connect()