Spaces:
Sleeping
Sleeping
liuxuan320
commited on
Commit
•
707698e
1
Parent(s):
803861a
Update app.py
Browse files
app.py
CHANGED
@@ -228,27 +228,87 @@
|
|
228 |
|
229 |
|
230 |
|
231 |
-
from flask import Flask, render_template_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
app = Flask(__name__)
|
234 |
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
<
|
241 |
-
<
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
|
|
|
228 |
|
229 |
|
230 |
|
231 |
+
# from flask import Flask, render_template_string
|
232 |
+
|
233 |
+
# app = Flask(__name__)
|
234 |
+
|
235 |
+
# @app.route("/")
|
236 |
+
# def home():
|
237 |
+
# # 使用 iframe 嵌入目标网站
|
238 |
+
# return render_template_string("""
|
239 |
+
# <!DOCTYPE html>
|
240 |
+
# <html lang="en">
|
241 |
+
# <head>
|
242 |
+
# <meta charset="UTF-8">
|
243 |
+
# <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
244 |
+
# <title>Embedded Website</title>
|
245 |
+
# </head>
|
246 |
+
# <body style="margin: 0; padding: 0; height: 100%; overflow: hidden;">
|
247 |
+
# <iframe src="http://71.132.14.167:6002/" frameborder="0" style="width: 100%; height: 100%; border: none;"></iframe>
|
248 |
+
# </body>
|
249 |
+
# </html>
|
250 |
+
# """)
|
251 |
+
|
252 |
+
# if __name__ == "__main__":
|
253 |
+
# app.run(host="0.0.0.0", port=7860)
|
254 |
+
|
255 |
+
from flask import Flask, render_template_string, request, jsonify
|
256 |
+
import requests
|
257 |
|
258 |
app = Flask(__name__)
|
259 |
|
260 |
+
# HTML模板,带按钮和数据展示区域
|
261 |
+
HTML_TEMPLATE = """
|
262 |
+
<!DOCTYPE html>
|
263 |
+
<html lang="en">
|
264 |
+
<head>
|
265 |
+
<meta charset="UTF-8">
|
266 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
267 |
+
<title>Random Data with Button</title>
|
268 |
+
<script>
|
269 |
+
async function fetchData() {
|
270 |
+
try {
|
271 |
+
const response = await fetch('/get-data');
|
272 |
+
const data = await response.json();
|
273 |
+
document.getElementById('number').innerText = data.number;
|
274 |
+
document.getElementById('message').innerText = data.message;
|
275 |
+
} catch (error) {
|
276 |
+
console.error('Error fetching data:', error);
|
277 |
+
document.getElementById('message').innerText = "Failed to fetch data!";
|
278 |
+
}
|
279 |
+
}
|
280 |
+
</script>
|
281 |
+
</head>
|
282 |
+
<body>
|
283 |
+
<h1>Data from Random Server</h1>
|
284 |
+
<button onclick="fetchData()">Get Random Data</button>
|
285 |
+
<p><strong>Random Number:</strong> <span id="number">N/A</span></p>
|
286 |
+
<p><strong>Message:</strong> <span id="message">Click the button to fetch data</span></p>
|
287 |
+
</body>
|
288 |
+
</html>
|
289 |
+
"""
|
290 |
+
|
291 |
+
REMOTE_SERVER_URL = 'http://120.76.41.157:6003/random'
|
292 |
+
|
293 |
+
@app.route('/')
|
294 |
+
def index():
|
295 |
+
# 返回带按钮的网页
|
296 |
+
return render_template_string(HTML_TEMPLATE)
|
297 |
+
|
298 |
+
@app.route('/get-data', methods=['GET'])
|
299 |
+
def get_data():
|
300 |
+
try:
|
301 |
+
# 向第一个服务器发送请求
|
302 |
+
response = requests.get(REMOTE_SERVER_URL)
|
303 |
+
response.raise_for_status() # 检查请求是否成功
|
304 |
+
|
305 |
+
# 返回 JSON 数据
|
306 |
+
return jsonify(response.json())
|
307 |
+
except requests.RequestException as e:
|
308 |
+
# 如果请求失败,返回错误信息
|
309 |
+
return jsonify({'number': 'Error', 'message': f'Failed to fetch data: {e}'}), 500
|
310 |
+
|
311 |
+
if __name__ == '__main__':
|
312 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|
313 |
+
|
314 |
|