Create a simple web app that has a file picker to select a local text file. Below the picker should be a process button that, when clicked, will read in the file, use regex to extract any valid IPv4 addresses, then print them out below
Drop files here
or click to upload
import streamlit as st
import re
st.title("IPv4 Address Extractor")
uploaded_file = st.file_uploader("Choose a text file", type=['txt'])
if uploaded_file is not None:
if st.button("Process File"):
# Read the contents of the file
content = uploaded_file.getvalue().decode("utf-8")
# Regular expression pattern for IPv4 addresses
ipv4_pattern = r'\b(?:\d{1,3}\.){3}\d{1,3}\b'
# Find all IPv4 addresses
ip_addresses = re.findall(ipv4_pattern, content)
# Filter valid IP addresses (additional validation)
valid_ips = []
for ip in ip_addresses:
octets = ip.split('.')
valid = True
for octet in octets:
if not (0 <= int(octet) <= 255):
valid = False
break
if valid:
valid_ips.append(ip)
if valid_ips:
st.write("Found IPv4 addresses:")
for ip in valid_ips:
st.write(f"- {ip}")
else:
st.write("No valid IPv4 addresses found in the file.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?