Files
python_test/smartassist/src/startservices.py
T
2024-07-17 14:27:18 +02:00

43 lines
1.5 KiB
Python

# Start all services
import subprocess
import threading
from backend import run_flask
import socket
def start_frontend():
host = 'localhost' # or the address of your server
port = 8000 # change to your server's port
# Use the socket module in Python to check whether a port is in use,
# which would indicate that a server is already running on that port.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((host, port))
print("No server is running on this host and port.")
# Start frontend (web server) as a separate process
subprocess.Popen(["python", "-m", "http.server", str(port)])
except socket.error as e:
if e.errno == 48:
print("Another server is already running on this host and port. Assumes it is a web server, will continue.")
else:
raise # Unexpected error, re-raise it so we can see the traceback
except Exception as e:
print(f"Failed to start frontend: {e}")
def start_backend():
try:
# Start backend as a separate thread
# threading.Thread(target=run_flask).start() # Flask's built-in server doesn't support running in a separate thread.
run_flask()
except Exception as e:
print(f"Failed to start backend: {e}")
if __name__ == '__main__':
start_frontend() # No need for this as the backend starts a web server on its own.
start_backend()