From: Petr Špaček Date: Fri, 1 Dec 2017 12:43:19 +0000 (+0100) Subject: scripts: make scripts/inet_pton.py compatible with Python 2 and Python 3 X-Git-Tag: v1.5.1~10^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e54acd9dd115725b54e5193d206a9063d01ec6e4;p=thirdparty%2Fknot-resolver.git scripts: make scripts/inet_pton.py compatible with Python 2 and Python 3 It is 2017 already, so Python 3 is the default. --- diff --git a/scripts/inet_pton.py b/scripts/inet_pton.py index 30bf06af6..6eea32ac2 100755 --- a/scripts/inet_pton.py +++ b/scripts/inet_pton.py @@ -1,19 +1,20 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 +""" +Print IP address to binary representation in Python hex format \x12 +""" -from socket import inet_pton,AF_INET6,AF_INET +from socket import inet_pton, AF_INET6, AF_INET import sys from binascii import hexlify -from string import find -if find(sys.argv[1], ":") == -1: - addr_type = AF_INET -else: - addr_type = AF_INET6 - -x = hexlify(inet_pton(addr_type, sys.argv[1])) +try: + arg = sys.argv[1] +except: + sys.exit('Usage: inet_pton.py ') -out = "" -for i in range(0, len(x) / 2): - out += "\\x" + x[i*2] + x[i*2+1] +if ':' in arg: + addr_type = AF_INET6 +else: + addr_type = AF_INET -print out +print(repr(inet_pton(addr_type, arg)).strip("'"))