Spaces:
Sleeping
Sleeping
| # combined_servers.py | |
| from flask import Flask, jsonify | |
| import requests | |
| import threading | |
| # Create the second server | |
| def run_second_server(): | |
| second_app = Flask(__name__) | |
| def get_data(): | |
| sample_data = { | |
| "message": "Hello from the second server!", | |
| "status": "success" | |
| } | |
| return jsonify(sample_data) | |
| second_app.run(port=5001) | |
| # Create the first server | |
| first_app = Flask(__name__) | |
| SECOND_SERVER_URL = 'http://localhost:5001/data' | |
| def fetch_data(): | |
| try: | |
| response = requests.get(SECOND_SERVER_URL) | |
| data = response.json() # Get JSON data from the second server | |
| return jsonify(data) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| if __name__ == '__main__': | |
| # Start the second server in a separate thread | |
| second_server_thread = threading.Thread(target=run_second_server) | |
| second_server_thread.start() | |
| # Start the first server | |
| first_app.run("0.0.0.0",port=7860) | |