Skip to content

Commit 9a67c03

Browse files
committed
Adding a runnable example of protocol integration.
1 parent fe2234f commit 9a67c03

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from twisted.internet.defer import Deferred
2+
from twisted.internet.endpoints import TCP4ServerEndpoint
3+
from twisted.internet.protocol import ServerFactory
4+
from twisted.internet.task import react
5+
6+
from parsley import makeProtocol
7+
from netstrings import grammar, NetstringSender
8+
9+
10+
class NetstringReverserState(object):
11+
def __init__(self, sender, parser):
12+
self.sender = sender
13+
14+
def connectionMade(self):
15+
pass
16+
17+
def connectionLost(self, reason):
18+
pass
19+
20+
def netstringReceived(self, string):
21+
self.sender.sendNetstring(string[::-1])
22+
23+
24+
NetstringReverser = makeProtocol(
25+
grammar, NetstringSender, NetstringReverserState)
26+
27+
28+
class NetstringReverserFactory(ServerFactory):
29+
protocol = NetstringReverser
30+
31+
32+
def main(reactor):
33+
server = TCP4ServerEndpoint(reactor, 1234)
34+
d = server.listen(NetstringReverserFactory())
35+
d.addCallback(lambda p: Deferred())
36+
return d
37+
38+
react(main, [])
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)