Add support for binding to both IPv4 and IPv6 addresses by hostname
This commit is contained in:
parent
456d563f92
commit
a2947a574c
1 changed files with 27 additions and 2 deletions
|
@ -70,6 +70,28 @@ class Server():
|
||||||
thread = threading.Thread(target=spawn)
|
thread = threading.Thread(target=spawn)
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _is_ipv6(value: str):
|
||||||
|
return value.find(':') >= 0
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _is_ipv4(value: str):
|
||||||
|
parts = value.split('.')
|
||||||
|
|
||||||
|
if len(parts) > 4 or len(parts) == 0:
|
||||||
|
return False
|
||||||
|
|
||||||
|
for part in parts:
|
||||||
|
if not part.isdecimal():
|
||||||
|
return False
|
||||||
|
|
||||||
|
num = int(part)
|
||||||
|
|
||||||
|
if num > 255:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if not self.config.has_section('listen'):
|
if not self.config.has_section('listen'):
|
||||||
raise ConfigSectionException('listen')
|
raise ConfigSectionException('listen')
|
||||||
|
@ -83,10 +105,13 @@ class Server():
|
||||||
listeners = list()
|
listeners = list()
|
||||||
|
|
||||||
for host in hosts:
|
for host in hosts:
|
||||||
if host.find(':') < 0:
|
if Server._is_ipv6(host):
|
||||||
|
listeners.append(self.listen(host, port, socket.AF_INET6))
|
||||||
|
elif Server._is_ipv4(host):
|
||||||
listeners.append(self.listen(host, port, socket.AF_INET))
|
listeners.append(self.listen(host, port, socket.AF_INET))
|
||||||
else:
|
else:
|
||||||
listeners.append(self.listen(host, port, socket.AF_INET6))
|
for af in (socket.AF_INET, socket.AF_INET6):
|
||||||
|
listeners.append(self.listen(host, port, af))
|
||||||
|
|
||||||
if len(listeners) == 0:
|
if len(listeners) == 0:
|
||||||
raise ConfigException('No listener hosts specified')
|
raise ConfigException('No listener hosts specified')
|
||||||
|
|
Loading…
Add table
Reference in a new issue