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 threading
 | 
			
		||||
import socket
 | 
			
		||||
| 
						 | 
				
			
			@ -6,7 +7,8 @@ import ssl
 | 
			
		|||
 | 
			
		||||
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.newsgroup import Newsgroup
 | 
			
		||||
from nntp.tiny.session   import Session
 | 
			
		||||
| 
						 | 
				
			
			@ -50,34 +52,44 @@ class Server():
 | 
			
		|||
        return listener
 | 
			
		||||
 | 
			
		||||
    def accept(self, listener):
 | 
			
		||||
        sock, addr = listener.accept()
 | 
			
		||||
        sock, addr = None, None
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            sock, addr = listener.accept()
 | 
			
		||||
        except ssl.SSLError as e:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        def spawn():
 | 
			
		||||
            session = Session(self, sock)
 | 
			
		||||
 | 
			
		||||
            try:
 | 
			
		||||
                session.handle()
 | 
			
		||||
            except (ssl.SSLError, ssl.SSLEOFError) as e:
 | 
			
		||||
            except ssl.SSLEOFError as e:
 | 
			
		||||
                pass
 | 
			
		||||
 | 
			
		||||
        thread = threading.Thread(target=spawn)
 | 
			
		||||
        thread.start()
 | 
			
		||||
 | 
			
		||||
    def run(self):
 | 
			
		||||
        port = int(self.config['listen']['port'])
 | 
			
		||||
        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'])
 | 
			
		||||
 | 
			
		||||
        listeners = list()
 | 
			
		||||
 | 
			
		||||
        if self.config.has_option('listen', 'host_inet'):
 | 
			
		||||
            host = self.config.get('listen', 'host_inet')
 | 
			
		||||
            listeners.append(self.listen(host, port, socket.AF_INET))
 | 
			
		||||
 | 
			
		||||
        if self.config.has_option('listen', 'host_inet6'):
 | 
			
		||||
            host = self.config.get('listen', 'host_inet6')
 | 
			
		||||
            listeners.append(self.listen(host, port, socket.AF_INET6))
 | 
			
		||||
        for host in hosts:
 | 
			
		||||
            if host.find(':') < 0:
 | 
			
		||||
                listeners.append(self.listen(host, port, socket.AF_INET))
 | 
			
		||||
            else:
 | 
			
		||||
                listeners.append(self.listen(host, port, socket.AF_INET6))
 | 
			
		||||
 | 
			
		||||
        if len(listeners) == 0:
 | 
			
		||||
            raise ConfigException("No listener hosts specified")
 | 
			
		||||
            raise ConfigException('No listener hosts specified')
 | 
			
		||||
 | 
			
		||||
        sel = selectors.DefaultSelector()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue