Sending binary pickled data through TCP
Nick Craig-Wood
nick at craig-wood.com
Fri Oct 13 04:30:10 EDT 2006
Paul Rubin <http> wrote:
> As for the network representation, DJB proposes this format:
> http://cr.yp.to/proto/netstrings.txt
Netstrings are cool and you'll find some python implementations if you
search.
But it is basically "number:string,", ie "12:hello world!,"
Or you could use escaping which is what I usually do. This has the
advantage that you don't need to know how long the data is in advance.
Eg, these are from a scheme which uses \t to seperate arguments and
\r or \n to seperate transactions. These are then escaped in the
actual data using these functions
def escape(s):
"""This escapes the string passed in, changing CR, LF, TAB and \\ into
\\r, \\n, \\t and \\\\"""
s = s.replace("\\", "\\\\")
s = s.replace("\r", "\\r")
s = s.replace("\n", "\\n")
s = s.replace("\t", "\\t")
return s
def unescape(s, _unescape_mapping = string.maketrans('tnr','\t\n\r'), _unescape_re = re.compile(r'\\([(rnt\\)])')):
"""This unescapes the string passed in, changing \\r, \\n, \\t and \\any_char into
CR, LF, TAB and any_char"""
def _translate(m):
return m.group(1).translate(_unescape_mapping)
return _unescape_re.sub(_translate, s)
(These functions have been through the optimisation mill which is why
they may not look immediately like how you might first think of
writing them!)
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list