File: netstrings.py

package info (click to toggle)
python-parsley 1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 948 kB
  • sloc: python: 9,897; makefile: 127
file content (17 lines) | stat: -rw-r--r-- 453 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
grammar = """

nonzeroDigit = digit:x ?(x != '0')
digits = <'0' | nonzeroDigit digit*>:i -> int(i)

netstring = digits:length ':' <anything{length}>:string ',' -> string

receiveNetstring = netstring:string -> receiver.netstringReceived(string)

"""

class NetstringSender(object):
    def __init__(self, transport):
        self.transport = transport

    def sendNetstring(self, string):
        self.transport.write('%d:%s,' % (len(string), string))