Home | Trees | Indices | Help |
|
---|
|
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license 2 3 # Copyright (C) 2003-2017 Nominum, Inc. 4 # 5 # Permission to use, copy, modify, and distribute this software and its 6 # documentation for any purpose with or without fee is hereby granted, 7 # provided that the above copyright notice and this permission notice 8 # appear in all copies. 9 # 10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 18 """IPv4 helper functions.""" 19 20 import struct 21 22 import dns.exception 23 from ._compat import binary_type 2426 """Convert an IPv4 address in binary form to text form. 27 28 *address*, a ``binary``, the IPv4 address in binary form. 29 30 Returns a ``text``. 31 """ 32 33 if len(address) != 4: 34 raise dns.exception.SyntaxError 35 if not isinstance(address, bytearray): 36 address = bytearray(address) 37 return ('%u.%u.%u.%u' % (address[0], address[1], 38 address[2], address[3]))3941 """Convert an IPv4 address in text form to binary form. 42 43 *text*, a ``text``, the IPv4 address in textual form. 44 45 Returns a ``binary``. 46 """ 47 48 if not isinstance(text, binary_type): 49 text = text.encode() 50 parts = text.split(b'.') 51 if len(parts) != 4: 52 raise dns.exception.SyntaxError 53 for part in parts: 54 if not part.isdigit(): 55 raise dns.exception.SyntaxError 56 if len(part) > 1 and part[0] == '0': 57 # No leading zeros 58 raise dns.exception.SyntaxError 59 try: 60 bytes = [int(part) for part in parts] 61 return struct.pack('BBBB', *bytes) 62 except: 63 raise dns.exception.SyntaxError64
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sat Dec 8 10:59:56 2018 | http://epydoc.sourceforge.net |