现在的位置: 首页 > 综合 > 正文

python:ip地址转换到数字

2014年11月07日 ⁄ 综合 ⁄ 共 412字 ⁄ 字号 评论关闭

在工作中需要将ip地址从字符串到整数。脚本语言可以提供快速的方式实现,在python中,:

代码如下

!/usr/bin/env python
# coding: utf-8

#这是一个IP转换为整数的程序
import struct,socket

def Ip2Int(ip):
    return struct.unpack("!L",socket.inet_aton(ip))[0]

 

if __name__ == '__main__':

  print Ip2Int('202.99.96.68')

 

 

这个库的使用可参考的手册:

http://www.python.org/doc/2.5.2/lib/module-socket.html

 

参考

http://hi.baidu.com/wg_wang/blog/item/69ba3330ec5a0b9fa8018e63.html

http://acen-chen.javaeye.com/blog/290163

抱歉!评论已关闭.