Last active
December 10, 2015 19:38
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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