Skip to content

Commit a6e1d7f

Browse files
committed
Update tutorial; fix up code listings.
1 parent b9a5647 commit a6e1d7f

File tree

4 files changed

+272
-196
lines changed

4 files changed

+272
-196
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from twisted.internet.defer import Deferred
2+
from twisted.internet.endpoints import TCP4ServerEndpoint
3+
from twisted.internet.protocol import Factory
4+
from twisted.internet.task import react
5+
6+
from parsley import makeProtocol, stack
7+
8+
9+
grammar = """
10+
11+
nonzeroDigit = digit:x ?(x != '0')
12+
digits = <'0' | nonzeroDigit digit*>:i -> int(i)
13+
14+
netstring = digits:length ':' <anything{length}>:string ',' -> string
15+
16+
receiveNetstring = netstring:string -> receiver.netstringReceived(string)
17+
18+
"""
19+
20+
21+
class NetstringReversalWrapper(object):
22+
def __init__(self, wrapped):
23+
self.wrapped = wrapped
24+
25+
def sendNetstring(self, string):
26+
self.wrapped.sendNetstring(string[::-1])
27+
28+
29+
class NetstringSender(object):
30+
def __init__(self, transport):
31+
self.transport = transport
32+
33+
def sendNetstring(self, string):
34+
self.transport.write('%d:%s,' % (len(string), string))
35+
36+
37+
class NetstringSplittingWrapper(object):
38+
def __init__(self, wrapped):
39+
self.wrapped = wrapped
40+
41+
def netstringReceived(self, string):
42+
splitpoint = len(string) // 2
43+
self.wrapped.netstringFirstHalfReceived(string[:splitpoint])
44+
self.wrapped.netstringSecondHalfReceived(string[splitpoint:])
45+
46+
def __getattr__(self, attr):
47+
return getattr(self.wrapped, attr)
48+
49+
50+
class SplitNetstringReceiver(object):
51+
currentRule = 'receiveNetstring'
52+
53+
def __init__(self, sender):
54+
self.sender = sender
55+
56+
def prepareParsing(self):
57+
pass
58+
59+
def finishParsing(self, reason):
60+
pass
61+
62+
def netstringFirstHalfReceived(self, string):
63+
self.sender.sendNetstring(string)
64+
65+
def netstringSecondHalfReceived(self, string):
66+
pass
67+
68+
69+
NetstringProtocol = makeProtocol(
70+
grammar,
71+
stack(NetstringReversalWrapper, NetstringSender),
72+
stack(NetstringSplittingWrapper, SplitNetstringReceiver))
73+
74+
class NetstringFactory(Factory):
75+
protocol = NetstringProtocol
76+
77+
78+
def main(reactor):
79+
server = TCP4ServerEndpoint(reactor, 1234)
80+
d = server.listen(NetstringFactory())
81+
d.addCallback(lambda p: Deferred()) # listen forever
82+
return d
83+
84+
85+
react(main, [])
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from twisted.internet.defer import Deferred
2+
from twisted.internet.endpoints import TCP4ServerEndpoint
3+
from twisted.internet.protocol import Factory
4+
from twisted.internet.task import react
5+
6+
from parsley import makeProtocol
7+
8+
9+
grammar = """
10+
11+
nonzeroDigit = digit:x ?(x != '0')
12+
digits = <'0' | nonzeroDigit digit*>:i -> int(i)
13+
14+
netstring = digits:length ':' <anything{length}>:string ',' -> string
15+
16+
receiveNetstring = netstring:string -> receiver.netstringReceived(string)
17+
18+
"""
19+
20+
21+
class NetstringSender(object):
22+
def __init__(self, transport):
23+
self.transport = transport
24+
25+
def sendNetstring(self, string):
26+
self.transport.write('%d:%s,' % (len(string), string))
27+
28+
29+
class NetstringReceiver(object):
30+
currentRule = 'receiveNetstring'
31+
32+
def __init__(self, sender):
33+
self.sender = sender
34+
35+
def prepareParsing(self):
36+
pass
37+
38+
def finishParsing(self, reason):
39+
pass
40+
41+
def netstringReceived(self, string):
42+
self.sender.sendNetstring(string)
43+
44+
45+
NetstringProtocol = makeProtocol(
46+
grammar, NetstringSender, NetstringReceiver)
47+
48+
49+
class NetstringFactory(Factory):
50+
protocol = NetstringProtocol
51+
52+
53+
def main(reactor):
54+
server = TCP4ServerEndpoint(reactor, 1234)
55+
d = server.listen(NetstringFactory())
56+
d.addCallback(lambda p: Deferred()) # listen forever
57+
return d
58+
59+
60+
react(main, [])
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from twisted.internet.defer import Deferred
2+
from twisted.internet.endpoints import TCP4ServerEndpoint
3+
from twisted.internet.protocol import Factory
4+
from twisted.internet.task import react
5+
6+
from parsley import makeProtocol
7+
8+
9+
grammar = """
10+
11+
nonzeroDigit = digit:x ?(x != '0')
12+
digits = <'0' | nonzeroDigit digit*>:i -> int(i)
13+
netstring :delimiter = digits:length delimiter <anything{length}>:string ',' -> string
14+
15+
colon = digits:length ':' <anything{length}>:string ',' -> receiver.netstringReceived(':', string)
16+
semicolon = digits:length ';' <anything{length}>:string ',' -> receiver.netstringReceived(';', string)
17+
18+
"""
19+
20+
21+
class NetstringSender(object):
22+
def __init__(self, transport):
23+
self.transport = transport
24+
25+
def sendNetstring(self, string):
26+
print 'received', repr(string)
27+
28+
29+
class NetstringReceiver(object):
30+
currentRule = 'colon'
31+
32+
def __init__(self, sender):
33+
self.sender = sender
34+
35+
def prepareParsing(self):
36+
pass
37+
38+
def finishParsing(self, reason):
39+
reason.printTraceback()
40+
41+
def netstringReceived(self, delimiter, string):
42+
self.sender.sendNetstring(string)
43+
if delimiter == ':':
44+
self.currentRule = 'semicolon'
45+
else:
46+
self.currentRule = 'colon'
47+
48+
49+
NetstringProtocol = makeProtocol(
50+
grammar, NetstringSender, NetstringReceiver)
51+
52+
53+
class NetstringFactory(Factory):
54+
protocol = NetstringProtocol
55+
56+
57+
def main(reactor):
58+
server = TCP4ServerEndpoint(reactor, 1234)
59+
d = server.listen(NetstringFactory())
60+
d.addCallback(lambda p: Deferred()) # listen forever
61+
return d
62+
63+
64+
react(main, [])

0 commit comments

Comments
 (0)