zhuhai111 commited on
Commit
0dda9a1
·
verified ·
1 Parent(s): e9d6dcf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -17
app.py CHANGED
@@ -1,11 +1,13 @@
1
  import os
2
  import subprocess
3
  import threading
4
- import gradio as gr
5
  import time
6
  import urllib.request
7
  import tarfile
8
  import zipfile
 
 
 
9
 
10
  # 下载并解压工具函数
11
  def download_and_extract(url, extract_to, is_zip=False):
@@ -67,33 +69,90 @@ def start_ssh_client():
67
  return subprocess.Popen(["python", "ssh_client.py"])
68
  return None
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  def setup_and_run():
 
71
  fluxbox_path = setup_fluxbox()
72
  vncserver_path = setup_tigervnc()
73
  novnc_path = setup_novnc()
 
 
 
 
74
  # 启动 Xvfb
75
- start_xvfb()
 
76
  time.sleep(2)
 
77
  # 启动 fluxbox
78
- start_fluxbox(fluxbox_path)
 
79
  time.sleep(2)
 
80
  # 启动 VNC server
81
- start_vncserver(vncserver_path)
 
82
  time.sleep(2)
 
83
  # 启动 noVNC
84
- start_novnc(novnc_path)
 
 
85
  # 启动 ssh_client.py
86
- # start_ssh_client()
87
-
88
- # 用线程后台启动桌面环境
89
- threading.Thread(target=setup_and_run, daemon=True).start()
90
-
91
- def hello():
92
- return "欢迎来到 Huggingface Space!已启动 Fluxbox+TigerVNC+noVNC+SSH 客户端。\n" \
93
- "请通过 Web VNC 访问桌面环境:\n" \
94
- "http://<your-space-url>:7860/vnc.html"
95
-
96
- demo = gr.Interface(fn=hello, inputs=None, outputs="text", title="Fluxbox TigerVNC Space")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  if __name__ == "__main__":
99
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
1
  import os
2
  import subprocess
3
  import threading
 
4
  import time
5
  import urllib.request
6
  import tarfile
7
  import zipfile
8
+ import http.server
9
+ import socketserver
10
+ import signal
11
 
12
  # 下载并解压工具函数
13
  def download_and_extract(url, extract_to, is_zip=False):
 
69
  return subprocess.Popen(["python", "ssh_client.py"])
70
  return None
71
 
72
+ # 创建简单的重定向页面到 noVNC
73
+ def create_redirect_html():
74
+ with open("index.html", "w") as f:
75
+ f.write("""<!DOCTYPE html>
76
+ <html>
77
+ <head>
78
+ <title>Fluxbox+TigerVNC Space</title>
79
+ <meta http-equiv="refresh" content="0; url=./vnc.html">
80
+ </head>
81
+ <body>
82
+ <p>重定向到 VNC 界面...</p>
83
+ <p><a href="./vnc.html">如果没有自动跳转,请点击这里</a></p>
84
+ </body>
85
+ </html>
86
+ """)
87
+
88
  def setup_and_run():
89
+ print("正在设置和启动桌面环境...")
90
  fluxbox_path = setup_fluxbox()
91
  vncserver_path = setup_tigervnc()
92
  novnc_path = setup_novnc()
93
+
94
+ # 创建重定向页面
95
+ create_redirect_html()
96
+
97
  # 启动 Xvfb
98
+ xvfb_proc = start_xvfb()
99
+ print("Xvfb 已启动")
100
  time.sleep(2)
101
+
102
  # 启动 fluxbox
103
+ fluxbox_proc = start_fluxbox(fluxbox_path)
104
+ print("Fluxbox 已启动")
105
  time.sleep(2)
106
+
107
  # 启动 VNC server
108
+ vnc_proc = start_vncserver(vncserver_path)
109
+ print("TigerVNC 已启动")
110
  time.sleep(2)
111
+
112
  # 启动 noVNC
113
+ novnc_proc = start_novnc(novnc_path)
114
+ print("noVNC 已启动在端口 7860,请访问 http://<your-space-url>:7860/vnc.html")
115
+
116
  # 启动 ssh_client.py
117
+ ssh_client_proc = start_ssh_client()
118
+ if ssh_client_proc:
119
+ print("ssh_client.py 已启动")
120
+
121
+ # 等待所有进程完成(实际上它们会一直运行)
122
+ try:
123
+ while True:
124
+ time.sleep(600) # 每10分钟检查一次
125
+
126
+ # 检查各个进程是否还在运行,如果不在则重启
127
+ if xvfb_proc.poll() is not None:
128
+ print("Xvfb 已终止,正在重启...")
129
+ xvfb_proc = start_xvfb()
130
+
131
+ if fluxbox_proc.poll() is not None:
132
+ print("Fluxbox 已终止,正在重启...")
133
+ fluxbox_proc = start_fluxbox(fluxbox_path)
134
+
135
+ if vnc_proc.poll() is not None:
136
+ print("TigerVNC 已终止,正在重启...")
137
+ vnc_proc = start_vncserver(vncserver_path)
138
+
139
+ if novnc_proc.poll() is not None:
140
+ print("noVNC 已终止,正在重启...")
141
+ novnc_proc = start_novnc(novnc_path)
142
+
143
+ if ssh_client_proc and ssh_client_proc.poll() is not None:
144
+ print("ssh_client.py 已终止,正在重启...")
145
+ ssh_client_proc = start_ssh_client()
146
+
147
+ except KeyboardInterrupt:
148
+ print("正在关闭所有进程...")
149
+ # 清理进程
150
+ for proc in [xvfb_proc, fluxbox_proc, vnc_proc, novnc_proc]:
151
+ if proc:
152
+ proc.terminate()
153
+ if ssh_client_proc:
154
+ ssh_client_proc.terminate()
155
 
156
  if __name__ == "__main__":
157
+ print("启动桌面环境:Fluxbox + TigerVNC + noVNC")
158
+ setup_and_run()