import webview import socket import sys ERROR_PAGE_CONTENT = """ Error

Service Not Available

No service is running on port 5000. Please start the Flask server first.

""" def is_port_in_use(port): """ 检查指定端口是否被占用。 参数: port (int): 要检查的端口号。 返回: bool: 如果端口被占用,则返回 True;否则返回 False。 """ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: return s.connect_ex(('127.0.0.1', port)) == 0 if __name__ == '__main__': port = 5000 if is_port_in_use(port): webview.create_window("Demo", f"http://127.0.0.1:{port}",fullscreen=False) else: webview.create_window("Error", html=ERROR_PAGE_CONTENT,fullscreen=False) webview.start()