Skip to content

Instantly share code, notes, and snippets.

@631068264
Created December 6, 2018 02:24
Show Gist options
  • Save 631068264/012bca1e3655979823c22615d8e10fb8 to your computer and use it in GitHub Desktop.
Save 631068264/012bca1e3655979823c22615d8e10fb8 to your computer and use it in GitHub Desktop.
python inet_ntoa and inet_aton
def inet_ntoa(n):
import socket, struct
if n:
try:
packed_value = struct.pack('!I', int(n))
ip = socket.inet_ntoa(packed_value)
return ip
except:
return None
return None
def inet_aton(ip):
import socket, struct
n = socket.inet_pton(socket.AF_INET, ip)
n = struct.unpack('!I', n)[0]
return n
def inet_ntoa(n):
import ipaddress
return str(ipaddress.ip_address(n))
def inet_aton(ip):
import ipaddress
return int(ipaddress.ip_address(ip))
@631068264
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment