jialiguo 发表于 2017-4-30 13:42:52

Python 获取本机外网IP

  


import re,urllib2
class Getmyip:
def getip(self):
try:
myip = self.visit("http://www.ip138.com/ip2city.asp")
except:
try:
myip = self.visit("http://www.bliao.com/ip.phtml")
except:
try:
myip = self.visit("http://www.whereismyip.com/")
except:
myip = "So sorry!!!"
return myip
def visit(self,url):
opener = urllib2.urlopen(url)
if url == opener.geturl():
str = opener.read()
return re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
getmyip = Getmyip()
localip = getmyip.getip()
print localip
 
页: [1]
查看完整版本: Python 获取本机外网IP