-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClient.py
More file actions
53 lines (51 loc) · 1.41 KB
/
Copy pathSocketClient.py
File metadata and controls
53 lines (51 loc) · 1.41 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import socket
import select
import threading
import sys
class Client:
def __init__(self, timeout, buffer_size):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(timeout)
self.buffer_size = buffer_size
def get_input(self):
self.user_input[0] = input(self.name + ": ")
self.user_input[1] = True
def connect(self, host, port):
self.socket.connect((host,port))
self.name = input("Select a nickname: ")
name = bytes("/!nick " + self.name, "utf-8")
self.socket.send(name)
self.user_input = [None, False]
t = threading.Thread(target=self.get_input)
t.start()
while 1:
socket_list = [self.socket]
ready_read, ready_write, error = select.select(socket_list, [], [], 0)
for sock in ready_read:
if sock == self.socket:
try:
data = self.socket.recv(self.buffer_size)
print(data)
if not data:
sock.close()
input("Disconnected. Press enter to quit.")
sys.exit()
else:
print(str(data[2:len(str(data))-2]))
except Exception as e:
print(e)
sock.close()
input("Disconnected. Press enter to quit.")
sys.exit()
if self.user_input[1]:
data = self.user_input[0]
self.socket.send(bytes(data, "utf-8"))
self.user_input[1] = 0
t = threading.Thread(target=self.get_input)
t.start()
bob = Client(2, 4096)
try:
bob.connect("localhost", 9001)
except Exception as e:
print(e)
input()