-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
executable file
·45 lines (35 loc) · 1.04 KB
/
client.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
import socket
import sys
import select
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port on the server given by the caller
server_address = ('localhost', 10000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
try:
sock.connect(server_address)
except:
print 'Unable to connect'
sys.exit()
print'Connected to remote host. You can start sending messages now.'
while 1:
# writable and exceptional don't have any corresponding variables because
# there's only one place to write to
inputs = [sys.stdin, sock]
readable, writable, exceptional = select.select(inputs, [], [])
for r in readable:
if r == sys.stdin:
# user typed in a message
msg = sys.stdin.readline()
sock.send(msg)
elif r == sock :
data = sock.recv(4096)
if not data :
# means the server disconnected
print '\nDisconnected from chat server'
sys.exit()
else :
#user entered a message
sys.stdout.write(str(data))
sys.stdout.write('You >')
sys.stdout.flush()