File size: 1,081 Bytes
4f0ca57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ee1009
4f0ca57
 
 
 
 
 
 
 
 
 
 
ef6cad0
4f0ca57
 
ef6cad0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# combined_servers.py

from flask import Flask, jsonify
import requests
import threading

# Create the second server
def run_second_server():
    second_app = Flask(__name__)

    @second_app.route('/data', methods=['GET'])
    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'

@first_app.route('/', methods=['GET'])
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)