-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketclientchat.py
66 lines (51 loc) · 2.12 KB
/
socketclientchat.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
# PS/ITC/14/0017
# Eric Opoku Jnr
import sys
from select import *
from socket import *
class client:
def __init__(self, host, port, username):
self.username = username
self.host = host
self.port = port
self.client_socket = socket(AF_INET, SOCK_STREAM)
#self.client_socket.settimeout(5)
try:
self.client_socket.connect((self.host,self.port))
#self._prompt()
except:
print("unable to connect to {} on port {}".format(self.host, self.port))
sys.exit()
print("Connected, you can start sending")
self.client_socket.send(self.username.encode('utf-8'))
self._prompt()
def run(self):
#print(self.client_socket)
while True:
connectors = [sys.stdin, self.client_socket]
ready_to_read = connectors
#ready_to_read, ready_to_write, in_error = select(connectors , [], [])
for sock in ready_to_read:
if sock == self.client_socket:
# handle incoming message send(), recv()
data = sock.recv(2048)
if not data:
print("\nDisconnected from chat server")
sys.exit()
else:
sys.stdout.write(data.decode('utf-8'))
sys.stdout.flush()
self._prompt()
else:
# send message
message = sys.stdin.readline()
self.client_socket.send(message.encode('utf-8'))
self._prompt()
def _prompt(self):
sys.stdout.write("\r=[{}]: ".format(self.username))
sys.stdout.flush()
if __name__=='__main__':
user = input("please enter your name: ")
c = client("192.168.109.1",9009, str(user))
#c._prompt()
c.run()