-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
44 lines (35 loc) · 1.33 KB
/
test.py
File metadata and controls
44 lines (35 loc) · 1.33 KB
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
43
44
from flask import Flask, jsonify
from veryyip import VeryyIP
import requests
# Initialize the Flask app
app = Flask(__name__)
# Initialize VeryyIP
ip = VeryyIP()
# Route to get the private (local) IP address
@app.route('/api/local-ip', methods=['GET'])
def get_local_ip():
local_ip = ip.get('private')
return jsonify({'local_ip': local_ip})
# Route to get the public IP address
@app.route('/api/public-ip', methods=['GET'])
def get_public_ip():
public_ip = ip.get('public')
return jsonify({'public_ip': public_ip})
# Route to display the IP addresses on the root path
@app.route('/', methods=['GET'])
def index():
# Get local IP address by calling /api/local-ip
local_ip_response = requests.get('http://localhost:5000/api/local-ip')
local_ip = local_ip_response.json().get('local_ip')
# Get public IP address by calling /api/public-ip
public_ip_response = requests.get('http://localhost:5000/api/public-ip')
public_ip = public_ip_response.json().get('public_ip')
# Display both IP addresses on the root page
return f'''
<h1>IP Address Information</h1>
<p><strong>Local IP Address:</strong> {local_ip}</p>
<p><strong>Public IP Address:</strong> {public_ip}</p>
'''
# Run the Flask application
if __name__ == '__main__':
app.run(debug=True)