assert 用法及获取IP 分类: python基础学习 2013-07-24 16:54 344人阅读 评论(0) 收藏

# -*- coding: utf -8 -*-

from socket import gethostname, gethostbyname
from exceptions import AssertionError

def get_local_ip():
    '''
       Return local host ip. 
    '''
    ip = gethostbyname(gethostname())
    assert ip != '127.0.1.1', 'No network connect, please the network connect again!'   # 抛出异常并提供描述信息
    return ip


try:
    print get_local_ip()
except AssertionError,  e:
    print e


assert 如果不满足条件,则抛出异常并提供描述信息

posted @ 2013-07-24 16:54  前行者2011  阅读(93)  评论(0编辑  收藏  举报