Skip to content

Instantly share code, notes, and snippets.

@meonkeys
Last active December 10, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meonkeys/4482362 to your computer and use it in GitHub Desktop.
Save meonkeys/4482362 to your computer and use it in GitHub Desktop.
Stand-alone sample code for converting some three-character octal escapes in Route 53 DNS domain names to their equivalent low-ASCII characters.
#!/usr/bin/python
# coding: utf-8
import re, unittest
def octalReplace(x):
c = int(x.group(1), 8)
if c > 0x20 and c < 0x2e or c > 0x2e and c < 0x7f:
return chr(c)
else:
return x.group(0)
def prettyDnsName(value):
return re.sub(r'\\(\d{3})', octalReplace, value)
class DecodeSomeOctalEscapesTest(unittest.TestCase):
def testShouldNotDecodeSmallLetterUWithDiaeresis(self):
# fübar
self.assertEqual(r'f\374bar', prettyDnsName(r'f\374bar'))
def testShouldNotDecodePeriod(self):
# f.bar
self.assertEqual(r'f\056bar', prettyDnsName(r'f\056bar'))
def testShouldDecodeAsterisk(self):
self.assertEqual('*.foo', prettyDnsName(r'\052.foo'))
def testShouldDecodeSmallLetterU(self):
self.assertEqual('fubar', prettyDnsName(r'f\165bar'))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment