-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2Client.py
39 lines (33 loc) · 1.1 KB
/
test2Client.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
import asyncore, socket, collections
class Client(asyncore.dispatcher):
def __init__(self, host_address, name):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.name = name
self.connect(host_address)
self.outbox = collections.deque()
print 'client started'
def say(self, message):
self.outbox.append(message)
print 'enqueued message'
def handle_write(self):
if not self.outbox:
return
message = self.outbox.popleft()
if len(message) > 1024:
raise ValueError('message too long')
self.send(message)
def handle_close(self):
print 'client: connection closed'
self.close()
def handle_read(self):
message = self.recv(1024)
if not message:
return
print 'received ', message
#self.send('Hello from client')
name = raw_input('write your nickname: ')
c = Client(('127.0.0.1', 1001), name)
c.say("Hello to all other worlds!")
# spawn other thread reading input here
asyncore.loop()