日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python编写IP地址与十进制IP转换脚本

發(fā)布時(shí)間:2025/4/5 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python编写IP地址与十进制IP转换脚本 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

IP地址與十進(jìn)制IP轉(zhuǎn)換

#!/usr/bin/env python

#encoding=utf-8


import re

import sys

import os


def ten_to_two(ten_num):

? ? two_str = ''


? ? while ten_num != 1:

? ? ? ? a = ten_num % 2

? ? ? ? two_str = two_str + str(a)

? ? ? ? ten_num = ten_num / 2

? ? else:

? ? ? ? two_str = two_str + str(1)

? ??

? ? two_str = two_str[::-1]

? ? if len(two_str) < 8:

? ? ? ? cover = (8 - len(two_str)) * str(0)

? ? ? ? two_str = cover + two_str


? ? return two_str


def two_to_ten(two_num):

? ? ten_int = 0

? ? indexlist = range(len(two_num))

? ? for i in indexlist:

? ? ? ? ten_int += int(two_num[i]) * 2 ** indexlist[-(i+1)]


? ? return str(ten_int)


def int_to_ipaddr(intnum):

? ? int_addr = int(intnum)

? ? twonum = ten_to_two(int_addr)


? ? if len(twonum) < 32:

? ? ? ? twonum = '0' * (32 - len(twonum)) + twonum


? ? ipaddr = ''

? ? for j in range(0, 32, 8):

? ? ? ? k = j + 8

? ? ? ? if j == 24:

? ? ? ? ? ? ipaddr += two_to_ten(twonum[j:k])

? ? ? ? else:

? ? ? ? ? ? ipaddr += two_to_ten(twonum[j:k]) + '.'


? ? return ipaddr


def ipaddr_to_int(ipaddress):

? ? iplist = ipaddress.split('.')

? ? ipstr = ''

? ? for i in iplist:

? ? ? ? ipstr += ten_to_two (int(i))

? ??

? ? if ipstr.startswith('0'):

? ? ? ? ipstr = re.sub('^0*', '', ipstr)


? ? tennum = two_to_ten(ipstr)

? ? return tennum




if len(sys.argv) < 2:

? ? print 'No action specified.'

? ? sys.exit()

elif len(sys.argv) == 2:

? ? if sys.argv[1].startswith('--'):

? ? ? ? option = sys.argv[1][2:]

? ? ? ? if option == 'version':

? ? ? ? ? ? print 'Version 1.1'

? ? ? ? elif option == 'help':

? ? ? ? ? ? print '''\

This program is used to convert the IP address.

\t-i ipaddress\n\t\tConvert the IP address.

\t\tlike ./iptrans.py 192.168.1.1

\t-if filename\n\t\tConvert the IP address's file.

\t\tlike ./iptrans.py ./ipfile?

\t-t int ipaddress\n\t\tConvert the int IP address.

\t\tlike ./iptrans.py 3232235777

\t-tf filename\n\t\tConvert the IP address's file.

\t\tlike ./iptrans.py ./intipfile

\t--help\n\t\tDisplay this help.

\t--version\n\t\tPrints the version number.'''

? ? else:

? ? ? ? print 'Unknown option.'

? ? ? ? sys.exit()

elif len(sys.argv) == 3:

? ? if sys.argv[1].startswith('-'):

? ? ? ? option = sys.argv[1][1:]

? ? ? ? if option == 'i':

? ? ? ? ? ? if sys.argv[2].count('.') == 3:

? ? ? ? ? ? ? ? ipaddress = sys.argv[2]

? ? ? ? ? ? ? ? intkey = ipaddr_to_int(ipaddress)

? ? ? ? ? ? ? ? print '%s\t%s' % (ipaddress, intkey)

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? print '%s is error.' % sys.argv[2]

? ? ? ? ? ? ? ? sys.exit()

? ? ? ? elif option == 'if':

? ? ? ? ? ? if os.path.isfile(sys.argv[2]) == True:

? ? ? ? ? ? ? ? filename = sys.argv[2]?

? ? ? ? ? ? ? ? f = open(filename, 'r')

? ? ? ? ? ? ? ? for line in f:

? ? ? ? ? ? ? ? ? ? ipaddress = line.replace('\n', '')

? ? ? ? ? ? ? ? ? ? intkey = ipaddr_to_int(ipaddress)

? ? ? ? ? ? ? ? ? ? print '%s\t%s' % (ipaddress, intkey)

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? print 'The %s is not exist.' % sys.argv[2]

? ? ? ? ? ? ? ? sys.exit()

? ? ? ? elif option == 't':

? ? ? ? ? ? intkey = sys.argv[2]

? ? ? ? ? ? if int(intkey) < 16777217:

? ? ? ? ? ? ? ? print '%s is error, must be great than 16777217' % sys.argv[2]

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ipaddress = int_to_ipaddr(intkey)

? ? ? ? ? ? ? ? print '%s\t%s' % (intkey, ipaddress)

? ? ? ? elif option == 'tf':

? ? ? ? ? ? if os.path.isfile(sys.argv[2]) == True:

? ? ? ? ? ? ? ? filename = sys.argv[2]?

? ? ? ? ? ? ? ? f = open(filename, 'r')

? ? ? ? ? ? ? ? for line in f:

? ? ? ? ? ? ? ? ? ? intkey = line.replace('\n', '')

? ? ? ? ? ? ? ? ? ? ipaddress = int_to_ipaddr(intkey)

? ? ? ? ? ? ? ? ? ? print '%s\t%s' % (intkey, ipaddress)

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? print 'The %s is not exist.' % sys.argv[2]

? ? ? ? ? ? ? ? sys.exit()

? ? ? ? else:

? ? ? ? ? ? print 'Unknown option.'

? ? ? ? ? ? sys.exit()

else:

? ?print 'Unknown option.'

? ?sys.exit()


轉(zhuǎn)載于:https://blog.51cto.com/gongniue/1825855

總結(jié)

以上是生活随笔為你收集整理的python编写IP地址与十进制IP转换脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。