mirror of
https://github.com/Dinnerbone/mcstatus.git
synced 2026-04-06 12:01:24 +08:00
fulfillment of requested changes.
This commit is contained in:
@@ -1,52 +1,15 @@
|
|||||||
import socket
|
import socket
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
from ipaddress import ip_address
|
||||||
|
|
||||||
def ip_type(address):
|
def ip_type(address):
|
||||||
try:
|
try:
|
||||||
socket.inet_pton(socket.AF_INET, address)
|
return ip_address(address).version
|
||||||
except AttributeError:
|
except ValueError:
|
||||||
try:
|
return None
|
||||||
socket.inet_aton(address)
|
|
||||||
except socket.error:
|
|
||||||
return False
|
|
||||||
return 4 if address.count('.') == 3 else None
|
|
||||||
except socket.error:
|
|
||||||
try:
|
|
||||||
socket.inet_pton(socket.AF_INET6, address)
|
|
||||||
return 6
|
|
||||||
except socket.error:
|
|
||||||
return None
|
|
||||||
return 4
|
|
||||||
|
|
||||||
def parse_address(address):
|
def parse_address(address):
|
||||||
parts = address.split(":")
|
tmp = urlparse("//"+address)
|
||||||
if len(parts) == 1:
|
if not tmp.hostname:
|
||||||
return (address, None)
|
raise ValueError("Invalid address '%s'" % address)
|
||||||
elif len(parts) == 2:
|
return (tmp.hostname, tmp.port)
|
||||||
try:
|
|
||||||
return (parts[0], int(parts[1]))
|
|
||||||
except:
|
|
||||||
raise ValueError("Invalid address '%s'" % address)
|
|
||||||
elif len(parts) < 10:
|
|
||||||
tmp = address
|
|
||||||
port = None
|
|
||||||
|
|
||||||
if len(parts[0]) and len(parts[-2]) and "[" == parts[0][0] and "]" == parts[-2][-1] :
|
|
||||||
if not parts[-1].isdigit():
|
|
||||||
raise ValueError("Invalid address '%s'" % address)
|
|
||||||
|
|
||||||
port = int(parts[-1])
|
|
||||||
parts[0] = parts[0][1:]
|
|
||||||
parts[-2] = parts[-2][:-1]
|
|
||||||
tmp = ':'.join(parts[0:-1])
|
|
||||||
else:
|
|
||||||
tmp = tmp.replace("[", "").replace("]", "")
|
|
||||||
|
|
||||||
if not ip_type(tmp) == 6:
|
|
||||||
raise ValueError("Invalid address '%s'" % address)
|
|
||||||
else:
|
|
||||||
return (tmp, port)
|
|
||||||
parts[0] = parts[0][1:]
|
|
||||||
parts[-2] = parts[0][:-1]
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ValueError("Invalid address '%s'" % address)
|
|
||||||
Reference in New Issue
Block a user