network.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:psystem 作者: gokhanm 项目源码 文件源码
def ip(self, interface_name, newip):
        """
            This function automatically will change netmask address.

            Checking interface first, if interface name found in Get().interfaces()
            validating Ipv4. After that applied ip address to interface

            interface_name: Applied Interface
            newip: New Ip Address

        """
        interface_check = Get().interfaces
        valid_ipv4 = validators.ipv4(newip)

        if not interface_name in interface_check:
            raise WrongInterfaceName("Wrong Interface Name %s" % interface_name)
        elif not valid_ipv4 is True:
            raise NotValidIPv4Address("Not Valid IPv4 Address %s" % newip)
        else:
            ifname = interface_name.encode(encoding='UTF-8')
            ipbytes = socket.inet_aton(newip)
            ifreq = struct.pack('16sH2s4s8s', ifname, AF_INET, b'\x00'*2, ipbytes, b'\x00'*8)
            fcntl.ioctl(self.sock, SIOCSIFADDR, ifreq)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号