-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathtwisted_chat_client.py
94 lines (64 loc) · 2.29 KB
/
twisted_chat_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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Code Listing #7
"""
Multiuser chat client using Twisted
"""
# twisted_chat_client.py
import sys
import socket
from twisted.internet import stdio, reactor, protocol
class ChatProtocol(protocol.Protocol):
""" Base protocol for chat """
def __init__(self, client):
self.output = None
# Client name: E.g: andy
self.client = client
self.prompt='[' + '@'.join((self.client, socket.gethostname().split('.')[0])) + ']> '
def input_prompt(self):
""" The input prefix for client """
sys.stdout.write(self.prompt)
sys.stdout.flush()
def dataReceived(self, data):
self.processData(data)
class ChatClientProtocol(ChatProtocol):
""" Chat client protocol """
def connectionMade(self):
print 'Connection made'
self.output.write(self.client + ":<handshake>")
def processData(self, data):
""" Process data received """
if not len(data.strip()):
return
self.input_prompt()
if self.output:
# Send data in this form to server
self.output.write(self.client + ":" + data)
class StdioClientProtocol(ChatProtocol):
""" Protocol which reads data from input and echoes
data to standard output """
def connectionMade(self):
# Create chat client protocol
chat = ChatClientProtocol(client=sys.argv[1])
chat.output = self.transport
# Create stdio wrapper
stdio_wrapper = stdio.StandardIO(chat)
# Connect to output
self.output = stdio_wrapper
print "Connected to server"
self.input_prompt()
def input_prompt(self):
# Since the output is directly connected
# to stdout, use that to write.
self.output.write(self.prompt)
def processData(self, data):
""" Process data received """
if self.output:
self.output.write('\n' + data)
self.input_prompt()
class StdioClientFactory(protocol.ClientFactory):
def buildProtocol(self, addr):
return StdioClientProtocol(sys.argv[1])
def main():
reactor.connectTCP("localhost", 3490, StdioClientFactory())
reactor.run()
if __name__ == '__main__':
main()