#_*_ encoding: utf-8 _*_
import urllib2,re
def getip():
ip_list=["http://1212.ip138.com/ic.asp/","http://www.whereismyip.com/"]
try:
opener = urllib2.urlopen(ip_list[0])
if ip_list[0] == opener.geturl():
tempstr = opener.read()
myip = re.search('\d+\.\d+\.\d+\.\d+', tempstr).group(0)
except:
try:
opener = urllib2.urlopen(ip_list[1])
if ip_list[1] == opener.geturl():
tempstr = opener.read()
myip = re.search('\d+\.\d+\.\d+\.\d+', tempstr).group(0)
except:
myip = "So sorry!!!"
return myip
localip = getip()
print localip
python获取外网ip的代码
最新推荐文章于 2022-11-04 00:00:49 发布