Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# combined_servers.py
|
| 2 |
+
|
| 3 |
+
from flask import Flask, jsonify
|
| 4 |
+
import requests
|
| 5 |
+
import threading
|
| 6 |
+
|
| 7 |
+
# Create the second server
|
| 8 |
+
def run_second_server():
|
| 9 |
+
second_app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
@second_app.route('/data', methods=['GET'])
|
| 12 |
+
def get_data():
|
| 13 |
+
sample_data = {
|
| 14 |
+
"message": "Hello from the second server!",
|
| 15 |
+
"status": "success"
|
| 16 |
+
}
|
| 17 |
+
return jsonify(sample_data)
|
| 18 |
+
|
| 19 |
+
second_app.run(port=5001)
|
| 20 |
+
|
| 21 |
+
# Create the first server
|
| 22 |
+
first_app = Flask(__name__)
|
| 23 |
+
|
| 24 |
+
SECOND_SERVER_URL = 'http://localhost:5001/data'
|
| 25 |
+
|
| 26 |
+
@first_app.route('/fetch', methods=['GET'])
|
| 27 |
+
def fetch_data():
|
| 28 |
+
try:
|
| 29 |
+
response = requests.get(SECOND_SERVER_URL)
|
| 30 |
+
data = response.json() # Get JSON data from the second server
|
| 31 |
+
return jsonify(data)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return jsonify({"error": str(e)}), 500
|
| 34 |
+
|
| 35 |
+
if __name__ == '__main__':
|
| 36 |
+
# Start the second server in a separate thread
|
| 37 |
+
second_server_thread = threading.Thread(target=run_second_server)
|
| 38 |
+
second_server_thread.start()
|
| 39 |
+
|
| 40 |
+
# Start the first server
|
| 41 |
+
first_app.run(port=5000)
|