-
-
Save 631068264/012bca1e3655979823c22615d8e10fb8 to your computer and use it in GitHub Desktop.
python inet_ntoa and inet_aton
This file contains 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
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 |
This file contains 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
def inet_ntoa(n): | |
import ipaddress | |
return str(ipaddress.ip_address(n)) | |
def inet_aton(ip): | |
import ipaddress | |
return int(ipaddress.ip_address(ip)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.cnblogs.com/gala/archive/2011/09/22/2184801.html