当前位置: 首页>>代码示例>>Python>>正文


Python Network.get_ips方法代码示例

本文整理汇总了Python中network.Network.get_ips方法的典型用法代码示例。如果您正苦于以下问题:Python Network.get_ips方法的具体用法?Python Network.get_ips怎么用?Python Network.get_ips使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在network.Network的用法示例。


在下文中一共展示了Network.get_ips方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import get_ips [as 别名]
    def run(self, dryrun):
        ensure_sudo()

        icv = InteractiveConfigValidation()
        icv.run()
        config = icv.get()

        network = Network(config['server'], config['port'], config['secret'], config['verify_ssl'])
        resp = network.get_ips()

        if resp == 'connection_error':
            print 'connection error'
            sys.exit(2)

        domains = [ d.split(' ') for d in resp.split('\n')]

        updated_hosts = []
        PD_BEGIN = '### PRIVATE DOMAINS BEGIN ###'
        PD_END =   '### PRIVATE DOMAINS END ###'

        if isfile('/etc/hosts'):
            PD_NOTSTARTED = 1
            PD_STARTED = 2
            PD_ENDED = 3
            state = PD_NOTSTARTED
            with open('/etc/hosts') as f:
                for line in f:
                    line = line[:-1] # remove \n
                    if state == PD_NOTSTARTED:
                        assert line not in [PD_END]
                        if line == PD_BEGIN:
                            state = PD_STARTED
                        else:
                            updated_hosts.append(line)
                    elif state == PD_STARTED:
                        assert line not in [PD_BEGIN]
                        if line == PD_END:
                            state = PD_ENDED
                    elif state == PD_ENDED:
                        assert line not in [PD_BEGIN, PD_END]
                        updated_hosts.append(line)
        else:
            # hosts did not exists create new one
            pass
        updated_hosts.append(PD_BEGIN)
        for domain, ip in domains:
            updated_hosts.append('%s\t%s' % (ip, domain))
        updated_hosts.append(PD_END)
        # adding a newline at the end of file
        updated_hosts.append('')

        updated_hosts_string = '\n'.join(updated_hosts)
        if dryrun:
            print updated_hosts_string
        else:
            with open('/etc/hosts', "w+") as f:
                f.write(updated_hosts_string)
开发者ID:nivwusquorum,项目名称:private-domains,代码行数:59,代码来源:methods.py


注:本文中的network.Network.get_ips方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。