aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/utils.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-09-24 11:21:48 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-09-24 11:21:48 +1200
commitb308824193342c11c88b8bad2645a5b09efcf48f (patch)
treea2820e799035f0915b8b101f8c2fc848c4d4a0d6 /netlib/utils.py
parent3a21e28bf13b5710639337fdc29741e9b6b71405 (diff)
downloadmitmproxy-b308824193342c11c88b8bad2645a5b09efcf48f.tar.gz
mitmproxy-b308824193342c11c88b8bad2645a5b09efcf48f.tar.bz2
mitmproxy-b308824193342c11c88b8bad2645a5b09efcf48f.zip
Create netlib.utils, move cleanBin and hexdump from libmproxy.utils.
Diffstat (limited to 'netlib/utils.py')
-rw-r--r--netlib/utils.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/netlib/utils.py b/netlib/utils.py
new file mode 100644
index 00000000..ea749545
--- /dev/null
+++ b/netlib/utils.py
@@ -0,0 +1,36 @@
+
+def cleanBin(s, fixspacing=False):
+ """
+ Cleans binary data to make it safe to display. If fixspacing is True,
+ tabs, newlines and so forth will be maintained, if not, they will be
+ replaced with a placeholder.
+ """
+ parts = []
+ for i in s:
+ o = ord(i)
+ if (o > 31 and o < 127):
+ parts.append(i)
+ elif i in "\n\r\t" and not fixspacing:
+ parts.append(i)
+ else:
+ parts.append(".")
+ return "".join(parts)
+
+
+def hexdump(s):
+ """
+ Returns a set of tuples:
+ (offset, hex, str)
+ """
+ parts = []
+ for i in range(0, len(s), 16):
+ o = "%.10x"%i
+ part = s[i:i+16]
+ x = " ".join("%.2x"%ord(i) for i in part)
+ if len(part) < 16:
+ x += " "
+ x += " ".join(" " for i in range(16 - len(part)))
+ parts.append(
+ (o, x, cleanBin(part, True))
+ )
+ return parts