如何使用python将作为str
的IP地址转换为十进制数,反之亦然?
例如,对于 IP 186.99.109.000 <type'str'>
,我希望有一个易于存储在数据库中的十进制或二进制形式,然后检索它。
将 IP 字符串转换为长整数:
import socket, struct
def ip2long(ip):
"""
Convert an IP string to long
"""
packedIP = socket.inet_aton(ip)
return struct.unpack("!L", packedIP)[0]
反之亦然:
>>> socket.inet_ntoa(struct.pack('!L', 2130706433))
'127.0.0.1'
以下是截至 2017-06 年所有选项的摘要。所有模块都是标准库的一部分,也可以通过pip install
安装。
IP地址模块
模块 ipaddress (doc( 是自 v3.3 以来标准库的一部分,但它也可以作为 Python v2.6、v2.7 的外部模块使用。
>>> import ipaddress
>>> int(ipaddress.ip_address('1.2.3.4'))
16909060
>>> str(ipaddress.ip_address(16909060))
'1.2.3.4'
>>> int(ipaddress.ip_address(u'1000:2000:3000:4000:5000:6000:7000:8000'))
21268296984521553528558659310639415296L
>>> str(ipaddress.ip_address(21268296984521553528558659310639415296L))
u'1000:2000:3000:4000:5000:6000:7000:8000'
无模块导入(仅限 IPv4(
无需导入,但仅适用于IPv4,并且代码比任何其他选项都长。
>>> ipstr = '1.2.3.4'
>>> parts = ipstr.split('.')
>>> (int(parts[0]) << 24) + (int(parts[1]) << 16) +
(int(parts[2]) << 8) + int(parts[3])
16909060
>>> ipint = 16909060
>>> '.'.join([str(ipint >> (i << 3) & 0xFF)
for i in range(4)[::-1]])
'1.2.3.4'
模块网络地址
netaddr是一个外部模块,但非常稳定,从Python 2.5(doc(开始可用。
>>> import netaddr
>>> int(netaddr.IPAddress('1.2.3.4'))
16909060
>>> str(netaddr.IPAddress(16909060))
'1.2.3.4'
>>> int(netaddr.IPAddress(u'1000:2000:3000:4000:5000:6000:7000:8000'))
21268296984521553528558659310639415296L
>>> str(netaddr.IPAddress(21268296984521553528558659310639415296L))
'1000:2000:3000:4000:5000:6000:7000:8000'
模块套接字和结构(仅限 ipv4(
这两个模块都是标准库的一部分,代码很短,有点神秘,只有IPv4。
>>> import socket, struct
>>> ipstr = '1.2.3.4'
>>> struct.unpack("!L", socket.inet_aton(ipstr))[0]
16909060
>>> ipint=16909060
>>> socket.inet_ntoa(struct.pack('!L', ipint))
'1.2.3.4'
在模块 netaddr
中使用类IPAddress
。
IPv4 str
-> int
:
print int(netaddr.IPAddress('192.168.4.54'))
# OUTPUT: 3232236598
IPv4 int
-> str
:
print str(netaddr.IPAddress(3232236598))
# OUTPUT: 192.168.4.54
IPv6 str
-> int
:
print int(netaddr.IPAddress('2001:0db8:0000:0000:0000:ff00:0042:8329'))
# OUTPUT: 42540766411282592856904265327123268393
IPv6 int
-> str
:
print str(netaddr.IPAddress(42540766411282592856904265327123268393))
# OUTPUT: 2001:db8::ff00:42:8329
从 Python 3.3 开始,就有了 ipaddress 模块来完成这项工作:https://docs.python.org/3/library/ipaddress.html。Python 2.x 的向后移植也可以在 PyPI 上使用。
用法示例:
import ipaddress
ip_in_int = int(ipaddress.ip_address('192.168.1.1'))
ip_in_hex = hex(ipaddress.ip_address('192.168.1.1'))
这里有一行答案:
import socket, struct
def ip2long_1(ip):
return struct.unpack("!L", socket.inet_aton(ip))[0]
<小时 /> def ip2long_2(ip):
return long("".join(["{0:08b}".format(int(num)) for num in ip.split('.')]), 2)
<小时 /> def ip2long_3(ip):
return long("".join(["{0:08b}".format(num) for num in map(int, ip.split('.'))]), 2)
<小时 />执行时间:
ip2long_1 => 0.0527065660363234 ( 最佳 (
ip2long_2 => 0.577211893924598
ip2long_3 => 0.5552745958088666
无需任何模块导入的单行解决方案:
ip2int = lambda ip: reduce(lambda a, b: (a << 8) + b, map(int, ip.split('.')), 0)
int2ip = lambda n: '.'.join([str(n >> (i << 3) & 0xFF) for i in range(0, 4)[::-1]])
例:
In [3]: ip2int('121.248.220.85')
Out[3]: 2046352469
In [4]: int2ip(2046352469)
Out[4]: '121.248.220.85'
将 IP 转换为整数:
python -c "print sum( [int(i)*2**(8*j) for i,j in zip( '10.20.30.40'.split('.'), [3,2,1,0]) ] )"
将中间人转换为 IP :
python -c "print '.'.join( [ str((169090600 >> 8*i) % 256) for i in [3,2,1,0] ])"
def ip2Hex(ip = None):
'''Returns IP in Int format from Octet form'''
#verifyFormat(ip)
digits=ip.split('.')
numericIp=0
count=0
for num in reversed(digits):
print "%d " % int(num)
numericIp += int(num) * 256 **(count)
count +=1
print "Numeric IP:",numericIp
print "Numeric IP Hex:",hex(numericIp)
ip2Hex('192.168.192.14')
ip2Hex('1.1.1.1')
ip2Hex('1.0.0.0')
这是一个
def ipv4_to_int(ip):
octets = ip.split('.')
count = 0
for i, octet in enumerate(octets):
count += int(octet) << 8*(len(octets)-(i+1))
return count
如果您的 IP 地址位于数据帧中,则可以使用库 DataPrep 中clean_ip
的函数。使用 pip install dataprep
安装数据准备。
from dataprep.clean import clean_ip
df = pd.DataFrame({"ip": ["186.99.109.000", "127.0.0.1", "1.2.3.4"]})
要转换为十进制格式,请将参数output_format
设置为 "integer"
:
df2 = clean_ip(df, "ip", output_format="integer")
# print(df2)
ip ip_clean
0 186.99.109.000 3127078144
1 127.0.0.1 2130706433
2 1.2.3.4 16909060
要转换为二进制格式,请将参数output_format
设置为 "binary"
:
df2 = clean_ip(df, "ip", output_format="binary")
# print(df2)
ip ip_clean
0 186.99.109.000 10111010011000110110110100000000
1 127.0.0.1 01111111000000000000000000000001
2 1.2.3.4 00000001000000100000001100000100
要转换回 IPv4,请将参数output_format
设置为 "compressed"
:
df = pd.DataFrame({"ip": [3127078144, 2130706433, 16909060]})
df2 = clean_ip(df, "ip", output_format="compressed")
# print(df2)
ip ip_clean
0 3127078144 186.99.109.0
1 2130706433 127.0.0.1
2 16909060 1.2.3.4
必须先将包含 IP 地址的字符串转换为字节或字节字符串,然后开始通信。根据下面的代码,您的错误将得到解决。确保您的代码总体上正常工作。
string = '192.168.1.102'
new_string = bytearray(string,"ascii")
ip_receiver = new_string
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(text.encode(), (ip_receiver, 5252))