通过python获取外网IP
#!/usr/bin/python
# -*- coding:utf-8 -*-
# filename:getLocalIp.py
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