获取访问网址出去ip

def getip():
    
    def visit(url):
        opener = urllib2.urlopen(url)
        if url == opener.geturl():
            str = opener.read()
        return re.search('\d+\.\d+\.\d+\.\d+', str).group(0)

    try:
        myip = visit("http://pv.sohu.com/cityjson?ie=utf-8")
    except:
        try:
            myip = visit("http://w.1616.net/chaxun/iptolocal.php")
        except:
            try:
                myip = visit("http://www.ip138.com/ip2city.asp")
            except:
                myip = "218.107.55.252"
    return myip

你可能感兴趣的:(获取访问网址出去ip)