原创

Python-IP地址转数字,数字转IP

IP地址转int格式用于存储数据库,进行格式转换方法

IP地址转化需要用到两个包socket、struct

1、引入包

import socket
import struct

2、IP地址转int函数

def ip_to_int(ip):
    return socket.ntohl(struct.unpack("I", socket.inet_aton(ip.strip(" ")))[0])
print(ip_to_int("192.168.0.1"))

>>3232235521

3、int转IP地址

def int_toip(num):
    return socket.inet_ntoa(struct.pack("!I", num))
print(int_toip(3232235521))
>>192.168.0.1
正文到此结束
评论

登录后才能发表评论 登录/注册

0评论
  • 还没有评论,快来抢沙发吧!