-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
43 lines (37 loc) · 1.1 KB
/
app.py
File metadata and controls
43 lines (37 loc) · 1.1 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
from flask import Flask, request, jsonify, render_template
import socket
app = Flask(__name__)
def ip_to_domain(ip_address):
try:
domain = socket.gethostbyaddr(ip_address)
return domain[0]
except socket.herror:
return None
def domain_to_ip(domain_name):
try:
ip = socket.gethostbyname(domain_name)
return ip
except socket.gaierror:
return None
@app.route('/')
def index():
return render_template('index.html')
@app.route('/convert', methods=['POST'])
def convert():
data = request.json
if data['type'] == 'ip_to_domain':
domain = ip_to_domain(data['input'])
if domain:
return jsonify({'result': domain})
else:
return jsonify({'error': 'Domain not found'}), 404
elif data['type'] == 'domain_to_ip':
ip = domain_to_ip(data['input'])
if ip:
return jsonify({'result': ip})
else:
return jsonify({'error': 'IP not found'}), 404
else:
return jsonify({'error': 'Invalid request'}), 400
if __name__ == '__main__':
app.run(debug=True)