From cd653236261a1bcfe90dcc6f1e053a11eaa43b76 Mon Sep 17 00:00:00 2001 From: Juhani Haverinen Date: Tue, 2 Aug 2016 00:04:50 +0300 Subject: [PATCH] Make neomi usable as a library by running listen(default_config) only if not imported --- neomi.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/neomi.py b/neomi.py index 335e83c..61bc610 100644 --- a/neomi.py +++ b/neomi.py @@ -6,13 +6,13 @@ import sys import threading import time -class config: None +class default_config: None -config.max_threads = 8192 -config.port = 7777 -config.recognised_selectors = ['0', '1', '5', '9', 'g', 'h', 'I', 's'] -config.request_max_size = 8192 -config.socket_timeout = 1 +default_config.max_threads = 8192 +default_config.port = 7777 +default_config.recognised_selectors = ['0', '1', '5', '9', 'g', 'h', 'I', 's'] +default_config.request_max_size = 8192 +default_config.socket_timeout = 1 # error(message) # Print error message to stderr @@ -184,13 +184,13 @@ def spawn_thread(sock, address, config): # Spawns worker threads to handle the connections def listen(config): # Get sockets that we listen to - listening_sockets = bind(port) + listening_sockets = bind(config.port) # Drop privileges, we don't need them after this drop_privileges() # If we got no sockets to listen to, die if listening_sockets == []: - die('Could not bind to port %i' % port) + die('Could not bind to port %i' % config.port) # Create a poll object for the listening sockets and a fd->socket map listening = select.poll() @@ -215,4 +215,5 @@ def listen(config): spawn_thread(conn, addr[0], config) -listen(config) +if __name__ == '__main__': + listen(default_config)