最方便的办法是使用ipaddress模块
import ipaddress
int(ipaddress.IPv4Address("192.168.0.1"))
不过最主要是记录一下直接转换的方式
>>> 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'
参考:https://stackoverflow.com/questions/9590965/convert-an-ip-string-to-a-number-and-vice-versa/9591005