import gradio as gr import IP import re class page: def __init__(self): self.ip = IP.IP() def validate_ip(self, ip): pattern = r'^(\d{1,3}\.){3}\d{1,3}/\d{1,2}$' if not re.match(pattern, ip): return "Invalid IP format. Please use format: xxx.xxx.xxx.xxx/xx" return None def run(self,share=False): with gr.Blocks(theme=gr.themes.Ocean()) as block: gr.Markdown("## 🌐 Network Calculator") with gr.Row(): with gr.Column(scale=3): ip_input = gr.Textbox( label="IP Address", placeholder="Enter IP address (e.g., 192.168.1.1/24)", show_label=True ) with gr.Column(scale=1): generate_btn = gr.Button("Calculate", variant="primary") with gr.Row(): with gr.Column(): ip_info = gr.Textbox(label="IP Address", interactive=False) subnet_mask = gr.Textbox(label="Subnet Mask", interactive=False) wildcard = gr.Textbox(label="Wildcard Mask", interactive=False) network_addr = gr.Textbox(label="Network Address", interactive=False) broadcast_addr = gr.Textbox(label="Broadcast Address", interactive=False) with gr.Column(): host_min = gr.Textbox(label="First Host", interactive=False) host_max = gr.Textbox(label="Last Host", interactive=False) total_hosts = gr.Textbox(label="Total Hosts", interactive=False) is_private = gr.Textbox(label="Private Network", interactive=False) addr_type = gr.Textbox(label="Address Type", interactive=False) with gr.Accordion("Binary Details", open=False): with gr.Row(): with gr.Column(): binary_subnet = gr.Textbox(label="Binary Subnet Mask", interactive=False) binary_wildcard = gr.Textbox(label="Binary Wildcard", interactive=False) with gr.Column(): binary_network = gr.Textbox(label="Binary Network Address", interactive=False) binary_broadcast = gr.Textbox(label="Binary Broadcast Address", interactive=False) def process_ip(ip): error = self.validate_ip(ip) if error: return {comp: error for comp in [ip_info, subnet_mask, wildcard, network_addr, broadcast_addr, host_min, host_max, total_hosts, is_private, addr_type, binary_subnet, binary_wildcard, binary_network, binary_broadcast]} self.ip.set_ip(ip) info = self.ip.Calculate() return { ip_info: info["IP Address"], subnet_mask: info["Subnet Mask"], wildcard: info["Wild Card"], network_addr: info["Network Address"], broadcast_addr: info["Broadcast Address"], host_min: info["Host Min"], host_max: info["Host Max"], total_hosts: str(info["Hosts"]), is_private: "Private Internet" if info["Private"] else "Public Internet", addr_type: info["Address Type"], binary_subnet: info["Binary Subnet Mask"], binary_wildcard: info["Binary Wild Card"], binary_network: info["Binary Network Address"], binary_broadcast: info["Binary Broadcast Address"] } generate_btn.click( fn=process_ip, inputs=ip_input, outputs=[ ip_info, subnet_mask, wildcard, network_addr, broadcast_addr, host_min, host_max, total_hosts, is_private, addr_type, binary_subnet, binary_wildcard, binary_network, binary_broadcast ] ) block.launch(share=share)