check, if cbpi port is already in use prior start and log error message

This commit is contained in:
avollkopf 2024-01-20 15:52:43 +01:00
parent 751f1721cd
commit 8b827268ce
2 changed files with 24 additions and 5 deletions

View file

@ -1,3 +1,3 @@
__version__ = "4.3.2.a5" __version__ = "4.3.2.a6"
__codename__ = "Winter Storm" __codename__ = "Winter Storm"

View file

@ -1,6 +1,7 @@
import asyncio import asyncio
import sys import sys
import socket
try: try:
from asyncio import set_event_loop_policy, WindowsSelectorEventLoopPolicy from asyncio import set_event_loop_policy, WindowsSelectorEventLoopPolicy
except ImportError: except ImportError:
@ -277,6 +278,22 @@ class CraftBeerPi:
self.app.add_routes([web.get('/', http_index), self.app.add_routes([web.get('/', http_index),
web.static('/static', os.path.join(os.path.dirname(__file__), "static"), show_index=True)]) web.static('/static', os.path.join(os.path.dirname(__file__), "static"), show_index=True)])
def testport(self, port=8000):
HOST = "localhost"
# Creates a new socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Try to connect to the given host and port
if sock.connect_ex((HOST, port)) == 0:
#print("Port " + str(port) + " is open") # Connected successfully
isrunning = True
else:
#print("Port " + str(port) + " is closed") # Failed to connect because port is in use (or bad host)
isrunning = False
# Close the connection
sock.close()
return isrunning
async def init_serivces(self): async def init_serivces(self):
self._print_logo() self._print_logo()
@ -304,7 +321,9 @@ class CraftBeerPi:
return self.app return self.app
def start(self): def start(self):
try: port=self.static_config.get("port",8000)
web.run_app(self.init_serivces(), port=self.static_config.get("port", 2202)) if not self.testport(port):
except Exception as e: web.run_app(self.init_serivces(), port=port)
print("Error Starting the server: {}".format(e)) else:
logging.error("Port {} is already in use! Please check, if server is already running (e.g. in automode)".format(port))
exit(1)