appDemo/amis-flask-pywebview-demo/window.py
murmur 350f52ead5 分离服务和窗口
服务添加文件锁
2024-07-04 23:14:15 +08:00

63 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import webview
import socket
import sys
ERROR_PAGE_CONTENT = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 96vh;
background-color: #f8d7da;
color: #721c24;
font-family: Arial, sans-serif;
}
.container {
text-align: center;
}
h1 {
font-size: 2em;
}
p {
font-size: 1.2em;
}
</style>
</head>
<body>
<div class="container">
<h1>Service Not Available</h1>
<p>No service is running on port 5000. Please start the Flask server first.</p>
</div>
</body>
</html>
"""
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()