-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (56 loc) · 2.09 KB
/
main.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask, Blueprint, request, jsonify
from networkinfo import Ipinfo
from portchecker import Checkport
from dnschecker import DNSChecker
#import os
import os
app_bp = Blueprint('api', __name__, url_prefix='/api')
@app_bp.route('/', methods=['GET'])
def greeting():
return jsonify({"message": "Welcome to the personal assistant api!"}), 200
@app_bp.route('/ipinfo', methods=['GET'])
def ipInfo():
try:
ipinfo_obj = Ipinfo(request.remote_addr)
ipinfo = ipinfo_obj.getInfo()
return jsonify({"ip": ipinfo['ip'], "city": ipinfo['city'], "country": ipinfo['country']}), 200
except Exception as error:
return str(error), 500
@app_bp.route('/ipinfo/<ip>', methods=['GET'])
def externalipInfo(ip):
try:
externalipinfo_obj = Ipinfo(ip)
ipinfo = externalipinfo_obj.getInfo()
return jsonify({"ip": ipinfo['ip'], "city": ipinfo['city'], "country": ipinfo['country']}), 200
except Exception as error:
return str(error), 500
@app_bp.route('/portchecker', methods=['GET'])
def local_portChecker():
ip = request.args.get('ip')
port = request.args.get('port')
if ip is None or port is None:
return jsonify({"error": "Please provide both 'ip' and 'port' parameters in the endpoint."}), 400
try:
port = int(port)
except ValueError:
return jsonify({"error": "Port must be an integer."}), 400
checker = Checkport(ip=ip, port=port)
try:
result = checker.portChecker()
return jsonify(result), 200
except Exception as error:
return jsonify({"error": str(error)}), 500
@app_bp.route('/dnschecker', methods=['GET'])
def dnsChecker():
dnsname = request.args.get('dnsname')
try:
checker = DNSChecker(dnsname)
return jsonify({"message": checker.dnslookUp()}), 200
except Exception as error:
return jsonify({"message": str(error)}), 400
if __name__ == "__main__":
app = Flask(__name__)
app.register_blueprint(app_bp)
host = os.environ.get('HOST')
port = int(os.environ.get('PORT'))
app.run(debug=True, host=host, port=port)