andy27367451 发表于 2017-4-22 08:09:48

python---assert用法

# -*- 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


$ python get_localhost_ip.py
No network connect, please the network connect again!
页: [1]
查看完整版本: python---assert用法