-
-
Notifications
You must be signed in to change notification settings - Fork 7
IP to FQDN API
A robust solution should:
- Provide meaningful error messages.
- Handle multiple concurrent requests without straining resources.
- Display progress if requested.
- Use a production-ready server like
gunicorn
. - Work behind a reverse proxy (for security and scalability).
For the purpose of this demonstration, I'll focus on points 1-3. Implementing a solution behind a reverse proxy using gunicorn
can be done but is out of scope for this concise guide. If you'd like that elaborated, let me know after this step.
from flask import Flask, request, jsonify
from flask_restful import Resource, Api
import subprocess
app = Flask(__name__)
api = Api(app)
DNS_SERVERS = []
# Load DNS servers from file
with open('dns_servers.txt', 'r') as f:
DNS_SERVERS = [line.strip() for line in f if line.strip()]
class ResolveIP(Resource):
def get(self, ip_address):
progress = {}
for index, dns_server in enumerate(DNS_SERVERS, 1):
try:
result = subprocess.check_output(['dig', '+short', '-x', ip_address, f'@{dns_server}'], stderr=subprocess.STDOUT).decode('utf-8').strip()
progress[dns_server] = result if result else "No record"
if result:
# Return as soon as we have a valid FQDN
return jsonify({"ip": ip_address, "fqdn": result, "progress": progress})
except subprocess.CalledProcessError:
progress[dns_server] = "Error during lookup"
return jsonify({"ip": ip_address, "error": "Unable to resolve", "progress": progress}), 404
api.add_resource(ResolveIP, '/resolve/<string:ip_address>')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
Enhancements:
-
Progress: The progress of the resolution is now maintained. For each DNS server queried, there is a status (either the resolved FQDN, "No record", or an error message). The progress is returned in the response.
-
Error Handling: More detailed error messages provide context on where the failure occurred, and each DNS server's status is returned.
-
Install
gunicorn
:pip install gunicorn
-
Run the app using
gunicorn
:gunicorn your_api_file_name:app -w 4 -b 0.0.0.0:5000
The
-w 4
argument means you are using 4 worker processes. Depending on your server's resources, you may increase or decrease this number.
others methods (web via python):
If you're not using gunicorn
, there are other methods to achieve a robust deployment for a Flask application. Here are a few alternatives:
waitress
is a pure Python WSGI server that's a good alternative to gunicorn
, especially for Windows environments.
- Install
waitress
:
pip install waitress
- Run your Flask app with
waitress
:
waitress-serve --listen=0.0.0.0:5000 your_api_file_name:app
uWSGI
is a powerful application server for Python and other languages.
- Install
uwsgi
:
pip install uwsgi
- Run your Flask app with
uwsgi
:
uwsgi --http 0.0.0.0:5000 --wsgi-file your_api_file_name.py --callable app
Flask's built-in server is mainly meant for development, but it can be used as a temporary measure:
export FLASK_APP=your_api_file_name.py
export FLASK_ENV=development
flask run --host=0.0.0.0 --port=5000
However, please note that Flask’s built-in server is not suitable for production as it doesn't scale well and can't handle multiple requests efficiently. For production deployments, you'll still want a more robust server setup.
For added security and performance, no matter which of the above solutions you choose, consider placing your application behind a reverse proxy like nginx
or Apache
. This provides benefits like SSL termination, load balancing, and more.
In any case, ensure that your production environment is properly secured, and take measures like disabling debug mode in Flask, setting up firewall rules, etc.