码迷,mamicode.com
首页 > 编程语言 > 详细

python ip整数与字符串转换

时间:2015-09-25 13:25:41      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

import socket,struct,sys,getopt,string

def doAnalysis(ifile, ofile):
    infile = open(ifile, ‘r‘)
    onfile = open(ofile, ‘w‘)
    for line in infile.readlines():
        info = line.split(‘\t‘)
        if len(info) >= 2:
            info[1] = socket.inet_ntoa(struct.pack(‘I‘, socket.htonl(string.atoi(info[1]))))
            onfile.write("%s\t%s\n"%(info[0], info[1]))
    infile.close()
    onfile.close()

if __name__ == ‘__main__‘:
    opts, args = getopt.getopt(sys.argv[1:], "i:o:")
    infile = ""
    ofile = ""
    for op, value in opts:
        if op == "-i":
            infile = value
        if op == "-o":
            ofile = value
    if infile == "" or ofile == "":
        print ‘input infile and ofile !!!!!‘
        sys.exit()
    else:
        doAnalysis(infile, ofile)



python ip整数与字符串转换

标签:

原文地址:http://my.oschina.net/u/659405/blog/511070

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!