Created
May 25, 2022 06:13
-
-
Save jingyuexing/2316dc15610a3e8585f0cda68f438dd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IP: | |
ip:str | |
integer:int | |
def __init__(self,address=0): | |
if isinstance(address,int): | |
self.integer = address | |
self.ip = self.toString(address) | |
if isinstance(address,str): | |
self.ip = address | |
self.integer = self.toInteger(address) | |
def parse(self,address,ip): | |
"""[summary] | |
[description] | |
Arguments: | |
address {[type]} -- [description] | |
ip {[type]} -- [description] | |
""" | |
pass | |
def toString(self,integer): | |
return f'{(integer>>24)&0xff}.{(integer>>16)&0xff}.{(integer>>8)&0xff}.{(integer>>0)&0xff}' | |
def toInteger(self,address="0.0.0.0"): | |
"""[summary] | |
[description] | |
Arguments: | |
address {[type]} -- [description] | |
Returns: | |
[type] -- [description] | |
""" | |
ipNumber = 0 | |
partIp = address.split('.') | |
i = 0 | |
for x in partIp: | |
ipNumber= ipNumber + int(x)*(0x100**(3-i)) | |
i=i+1 | |
return ipNumber | |
def range(self,start:int = 0,end:int = 0,ipType=""): | |
IPRange = { | |
"A":["0.0.0.0","127.255.255.255"], | |
"B":["128.0.0.0","191.255.255.255"], | |
"C":["192.0.0.0","223.255.255.255"], | |
"D":["224.0.0.0","239.255.255.255"], | |
"E":["240.0.0.0","247.255.255.255"] | |
} | |
this = self | |
class IpRangeIter: | |
def __iter__(self): | |
self.count = start | |
return self | |
def __next__(self): | |
if(ipType is 'A'): | |
this.integer = this.toInteger(IPRange['A'][0]) | |
end = this.toInteger(IPRange['A'][1]) | |
elif(ipType is 'B'): | |
this.integer = this.toInteger(IPRange['B'][0]) | |
end = this.toInteger(IPRange['B'][1]) | |
elif(ipType is 'C'): | |
this.integer = this.toInteger(IPRange['C'][0]) | |
end = this.toInteger(IPRange['C'][1]) | |
elif(ipType is 'D'): | |
this.integer = this.toInteger(IPRange['D'][0]) | |
end = this.toInteger(IPRange['D'][1]) | |
elif(ipType is 'E'): | |
this.integer = this.toInteger(IPRange['E'][0]) | |
end = this.toInteger(IPRange['E'][1]) | |
if(self.count < end): | |
integerVal = this.integer + self.count | |
val = this.toString(integerVal) | |
self.count += 1 | |
return (val,integerVal) | |
else: | |
raise StopIteration() | |
return iter(IpRangeIter()) | |
if __name__ == '__main__': | |
for ip,integer in IP().range(ipType='C'): | |
print(ip,integer) | |
for ip,integer in IP("127.0.0.1").range(start=0,end=255): | |
print(ip,integer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment