Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def start(self, port, *args, **config):
config.update(self.config)
config['server.socket_port'] = port
portend.free('localhost', port)
server = self[port] = subprocess.Popen((sys.executable, '-m', self.module, '-c', json.dumps(config)) + args)
portend.occupied('localhost', port)
server.started = time.time()
assert not server.poll()
return clients.Resource('http://localhost:{}'.format(port))
def check_port_availability(host, port):
"""
Make sure the port is available for the server to start.
"""
try:
portend.free(host, port, timeout=PORT_AVAILABILITY_CHECK_TIMEOUT)
except portend.Timeout:
# Bypass check when socket activation is used
# https://manpages.debian.org/testing/libsystemd-dev/sd_listen_fds.3.en.html#ENVIRONMENT
if not os.environ.get("LISTEN_PID", None):
# Port is occupied
logger.error(
"Port {} is occupied.\n"
"Please check that you do not have other processes "
"running on this port and try again.\n".format(port)
)
sys.exit(1)
def start(self):
"""Start the HTTP server."""
if self.running:
self.bus.log('Already serving on %s' % self.description)
return
self.interrupt = None
if not self.httpserver:
raise ValueError('No HTTP server has been created.')
if not os.environ.get('LISTEN_PID', None):
# Start the httpserver in a new thread.
if isinstance(self.bind_addr, tuple):
portend.free(*self.bind_addr, timeout=Timeouts.free)
import threading
t = threading.Thread(target=self._start_http_thread)
t.setName('HTTPServer ' + t.getName())
t.start()
self.wait()
self.running = True
self.bus.log('Serving on %s' % self.description)
start.priority = 75
def start(self):
"""Start the HTTP server."""
if self.running:
self.bus.log('Already serving on %s' % self.description)
return
self.interrupt = None
if not self.httpserver:
raise ValueError('No HTTP server has been created.')
if not os.environ.get('LISTEN_PID', None):
# Start the httpserver in a new thread.
if isinstance(self.bind_addr, tuple):
portend.free(*self.bind_addr, timeout=Timeouts.free)
import threading
t = threading.Thread(target=self._start_http_thread)
t.setName('HTTPServer ' + t.getName())
t.start()
self.wait()
self.running = True
self.bus.log('Serving on %s' % self.description)
start.priority = 75
def find_free_port(host, currentport):
""" Return a free port, 0 when nothing is free """
n = 0
while n < 10 and currentport <= 49151:
try:
portend.free(host, currentport, timeout=0.025)
return currentport
except:
currentport += 5
n += 1
return 0
abort_and_show_error(browserhost, cherryport)
except:
abort_and_show_error(browserhost, cherryport, '49')
# Windows instance is reachable through registry
url = None
if sabnzbd.WIN32 and not new_instance:
url = get_connection_info()
if url and check_for_sabnzbd(url, upload_nzbs, autobrowser):
exit_sab(0)
# SSL
if enable_https:
port = https_port or cherryport
try:
portend.free(browserhost, port, timeout=0.05)
except IOError as error:
if str(error) == 'Port not bound.':
pass
else:
if not url:
url = 'https://%s:%s%s/api?' % (browserhost, port, sabnzbd.cfg.url_base())
if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser):
# Bail out if we have fixed our ports after first start-up
if sabnzbd.cfg.fixed_ports():
abort_and_show_error(browserhost, cherryport)
# Find free port to bind
newport = find_free_port(browserhost, port)
if newport > 0:
# Save the new port
if https_port:
https_port = newport
# Determine web host address
cherryhost, cherryport, browserhost, https_port = get_webhost(cherryhost, cherryport, https_port)
enable_https = sabnzbd.cfg.enable_https()
# When this is a daemon, just check and bail out if port in use
if sabnzbd.DAEMON:
if enable_https and https_port:
try:
portend.free(cherryhost, https_port, timeout=0.05)
except IOError:
abort_and_show_error(browserhost, cherryport)
except:
abort_and_show_error(browserhost, cherryport, '49')
try:
portend.free(cherryhost, cherryport, timeout=0.05)
except IOError:
abort_and_show_error(browserhost, cherryport)
except:
abort_and_show_error(browserhost, cherryport, '49')
# Windows instance is reachable through registry
url = None
if sabnzbd.WIN32 and not new_instance:
url = get_connection_info()
if url and check_for_sabnzbd(url, upload_nzbs, autobrowser):
exit_sab(0)
# SSL
if enable_https:
port = https_port or cherryport
try:
# Set root folders for HTTPS server file paths
sabnzbd.cfg.set_root_folders2()
if ipv6_hosting is not None:
sabnzbd.cfg.ipv6_hosting.set(ipv6_hosting)
# Determine web host address
cherryhost, cherryport, browserhost, https_port = get_webhost(cherryhost, cherryport, https_port)
enable_https = sabnzbd.cfg.enable_https()
# When this is a daemon, just check and bail out if port in use
if sabnzbd.DAEMON:
if enable_https and https_port:
try:
portend.free(cherryhost, https_port, timeout=0.05)
except IOError:
abort_and_show_error(browserhost, cherryport)
except:
abort_and_show_error(browserhost, cherryport, '49')
try:
portend.free(cherryhost, cherryport, timeout=0.05)
except IOError:
abort_and_show_error(browserhost, cherryport)
except:
abort_and_show_error(browserhost, cherryport, '49')
# Windows instance is reachable through registry
url = None
if sabnzbd.WIN32 and not new_instance:
url = get_connection_info()
if url and check_for_sabnzbd(url, upload_nzbs, autobrowser):