Allow specifying arbitrary number of listener hosts
This commit is contained in:
parent
9cb8b2e0e4
commit
456d563f92
1 changed files with 24 additions and 12 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import re
|
||||||
import enum
|
import enum
|
||||||
import threading
|
import threading
|
||||||
import socket
|
import socket
|
||||||
|
@ -6,7 +7,8 @@ import ssl
|
||||||
|
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
|
||||||
from nntp.tiny.config import ConfigException
|
from nntp.tiny.config import (
|
||||||
|
ConfigException, ConfigSectionException, ConfigValueException)
|
||||||
from nntp.tiny.db import Database
|
from nntp.tiny.db import Database
|
||||||
from nntp.tiny.newsgroup import Newsgroup
|
from nntp.tiny.newsgroup import Newsgroup
|
||||||
from nntp.tiny.session import Session
|
from nntp.tiny.session import Session
|
||||||
|
@ -50,34 +52,44 @@ class Server():
|
||||||
return listener
|
return listener
|
||||||
|
|
||||||
def accept(self, listener):
|
def accept(self, listener):
|
||||||
|
sock, addr = None, None
|
||||||
|
|
||||||
|
try:
|
||||||
sock, addr = listener.accept()
|
sock, addr = listener.accept()
|
||||||
|
except ssl.SSLError as e:
|
||||||
|
return
|
||||||
|
|
||||||
def spawn():
|
def spawn():
|
||||||
session = Session(self, sock)
|
session = Session(self, sock)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session.handle()
|
session.handle()
|
||||||
except (ssl.SSLError, ssl.SSLEOFError) as e:
|
except ssl.SSLEOFError as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
thread = threading.Thread(target=spawn)
|
thread = threading.Thread(target=spawn)
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
if not self.config.has_section('listen'):
|
||||||
|
raise ConfigSectionException('listen')
|
||||||
|
|
||||||
|
if not self.config.has_option('listen', 'host'):
|
||||||
|
raise ConfigValueException('listen', 'host')
|
||||||
|
|
||||||
|
hosts = re.split(r'\s*,\s*', self.config['listen']['host'])
|
||||||
port = int(self.config['listen']['port'])
|
port = int(self.config['listen']['port'])
|
||||||
|
|
||||||
listeners = list()
|
listeners = list()
|
||||||
|
|
||||||
if self.config.has_option('listen', 'host_inet'):
|
for host in hosts:
|
||||||
host = self.config.get('listen', 'host_inet')
|
if host.find(':') < 0:
|
||||||
listeners.append(self.listen(host, port, socket.AF_INET))
|
listeners.append(self.listen(host, port, socket.AF_INET))
|
||||||
|
else:
|
||||||
if self.config.has_option('listen', 'host_inet6'):
|
|
||||||
host = self.config.get('listen', 'host_inet6')
|
|
||||||
listeners.append(self.listen(host, port, socket.AF_INET6))
|
listeners.append(self.listen(host, port, socket.AF_INET6))
|
||||||
|
|
||||||
if len(listeners) == 0:
|
if len(listeners) == 0:
|
||||||
raise ConfigException("No listener hosts specified")
|
raise ConfigException('No listener hosts specified')
|
||||||
|
|
||||||
sel = selectors.DefaultSelector()
|
sel = selectors.DefaultSelector()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue