python_streamlit_子网划分计算器

import pandas as pd
import ipaddress
import streamlit as st

class compute:
    def __init__(self):
        st.title(':red[Welcome] :sunglasses:')  # 创建title,可以链接
        self.iplist_ipv4 = []
        self.iplist_ipv6 = []
        self.iplist_ipv4_subnet = []
        self.iplist_ipv6_subnet = []

    def ipv4(self):
        st.sidebar.header(":red[IP计算器]")
        net4 = st.sidebar.text_input("请输入ipv4地址",help="示例:1.1.1.1/24")  # 网页上新建输入窗口
        try:
            net4_result = ipaddress.IPv4Network(net4, strict=False)
            series_ipv4 = [net4_result.with_prefixlen,net4_result.with_hostmask ,str(net4_result[1]) + str("--") + str(net4_result[-2])]
            self.df_ipv4 = pd.DataFrame({"ipv4信息": series_ipv4}, index=["网络地址", "反码地址", "可用地址"])
            st.dataframe(self.df_ipv4, width=500)
            # 子网划分
            choose = st.sidebar.checkbox("是否进行ipv4子网划分")
            # st.write(net4_result.prefixlen)
            if choose == True:
                cidr = st.sidebar.number_input("请选择划分的ipv4子网", min_value=1, max_value=31)
                if cidr - net4_result.prefixlen < 15:
                    base_prefix = "/" + str(cidr)
                    num_cidr_ipv4 = 2 ** (32 - int(cidr))
                    all_host_number = net4_result.num_addresses # 获取所有ip地址的数量

                    for num in range(0, all_host_number // num_cidr_ipv4):
                        self.iplist_ipv4_subnet.append(str(net4_result[num_cidr_ipv4*num]) + base_prefix)
                    st.write(":red[ipv4子网详情如下]")
                    df_ipv4 = pd.DataFrame(data=self.iplist_ipv4_subnet, columns=["子网划分结果"])
                    #st.write(self.iplist_ipv4_subnet)
                    st.dataframe(df_ipv4,width=500)
                else:
                    st.write(":red[子网数量过大]")
            else:
                pass
        except ValueError:
            pass
            # st.sidebar.caption(":red[框框里头要正确的地址]:sob:")
        except IndexError:
            st.sidebar.caption(":red[No Prefix] :sob:")

    def ipv6(self):
        net6 = st.sidebar.text_input("请输入ipv6地址",help="示例:fe::/120")
        try:
            net6_result = ipaddress.IPv6Network(net6, strict=False)
            series_ipv6 = [net6_result.with_prefixlen, net6_result.with_hostmask, str(net6_result[1]) + str("--") + str(net6_result[-1])]
            self.df_ipv6 = pd.DataFrame({"ipv6信息": series_ipv6}, index=["网络地址", "反码地址", "可用地址"])
            st.dataframe(self.df_ipv6, width=500)
            # ipv6子网划分
            choose_ipv6 = st.sidebar.checkbox("是否进行ipv6子网划分")
            # st.write(choose)
            if choose_ipv6 == True:
                cidr_ipv6 = st.sidebar.number_input("请选择划分的ipv6子网",min_value=1,max_value=127)
                if cidr_ipv6 - net6_result.prefixlen < 15:
                    base_prefix = "/" + str(cidr_ipv6)
                    num_cidr_ipv6 = 2 ** (128 - int(cidr_ipv6))
                    all_host_number_ipv6 = net6_result.num_addresses

                    for num in range(0, all_host_number_ipv6 // num_cidr_ipv6):
                        self.iplist_ipv6_subnet.append(str(net6_result[num_cidr_ipv6 * num]) + base_prefix)
                    st.write(":red[ipv6子网详情如下]")
                    df_ipv6 = pd.DataFrame(data=self.iplist_ipv6_subnet,columns=["子网划分结果"])
                    st.dataframe(data=df_ipv6,width=500)
                    #st.write(self.iplist_ipv6_subnet)
                else:
                    st.write(":red[子网数量过大]")
            else:
                pass

        except ValueError:
            pass
            #st.sidebar.caption(":red[框框里头要正确的地址] :sob:")
        except IndexError:
            st.sidebar.caption(":red[No Prefix] :sob:")

            # st.header(":red[No IP Address] :sob:")

yc = compute()
yc.ipv4()
yc.ipv6()

 

posted @ 2023-03-29 23:17  小菜鸟起飞ing  阅读(81)  评论(0编辑  收藏  举报