In today's interconnected world, IP addresses and networks are critical components that facilitate communication between devices. Understanding and manipulating IP addresses programmatically can be quite useful. To help you do just that, I'll walk you through building a Flask-based API that performs some useful IPv4-related functions.
What We Will Cover
- Validating an IPv4 address
- Obtaining the network and broadcast addresses of a CIDR block
- Checking if two IP addresses are in the same subnet
Prerequisites
- Python installed on your machine
- Basic understanding of Flask and RESTful APIs
pip install Flask
to install Flask if you haven't already
API Endpoints
1. Validating an IPv4 Address
The first endpoint we'll create validates an IPv4 address.
- Endpoint:
/validate_ipv4
- Method:
GET
- Parameters:
ip
(the IP address to validate)
2. Getting Network Information
The second endpoint provides the network and broadcast addresses of a given CIDR block.
- Endpoint:
/network_info
- Method:
GET
- Parameters:
cidr
(the CIDR block)
3. Checking if Two IP Addresses are in the Same Subnet
The third endpoint checks if two given IP addresses fall within the same CIDR block.
- Endpoint:
/same_subnet
- Method:
GET
- Parameters:
ip1
,ip2
(the IP addresses to check),cidr
(the CIDR block)
Code Implementation
from flask import Flask, request, jsonify
from ipaddress import ip_address, ip_network
app = Flask(__name__)
@app.route("/validate_ipv4", methods=["GET"])
def validate_ipv4():
ip = request.args.get("ip")
try:
ip_address(ip)
return jsonify({"valid": True})
except ValueError:
return jsonify({"valid": False}), 400
@app.route("/network_info", methods=["GET"])
def network_info():
cidr = request.args.get("cidr")
try:
network = ip_network(cidr, strict=False)
return jsonify({"network_address": str(network.network_address), "broadcast_address": str(network.broadcast_address)})
except ValueError:
return jsonify({"error": "Invalid CIDR"}), 400
@app.route("/same_subnet", methods=["GET"])
def same_subnet():
ip1 = request.args.get("ip1")
ip2 = request.args.get("ip2")
cidr = request.args.get("cidr")
try:
network = ip_network(cidr, strict=False)
return jsonify({"same_subnet": ip_address(ip1) in network and ip_address(ip2) in network})
except ValueError:
return jsonify({"error": "Invalid IP or CIDR"}), 400
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Running the API
- Save the code to a file named
app.py
. - Open your terminal and run
python app.py
. - The API will be accessible at
http://localhost:5000
.
Testing the API
Here's how to test each endpoint:
- Validating IPv4: Navigate to
http://localhost:5000/validate_ipv4?ip=192.168.1.1
. - Network Info: Navigate to
http://localhost:5000/network_info?cidr=192.168.1.0/24
. - Same Subnet: Navigate to
http://localhost:5000/same_subnet?ip1=192.168.1.1&ip2=192.168.1.2&cidr=192.168.1.0/24
.
And that's it! You now have a working API for IPv4-related tasks. This is a simple example, but you can easily extend it to include more advanced features and functionalities. Happy coding!
No comments:
Post a Comment