http://bbs.bccn.net/thread-298418-1-2.html
http://xiaolin0199.javaeye.com/blog/585531
#-*- coding: gb2312 -*- print "本机的私网IP地址为:" + re.search('\d+\.\d+\.\d+\.\d+',Popen('ipconfig', stdout=PIPE).stdout.read()).group(0) 运行结果如下: 本机的私网IP地址为:192.168.1.21 PYTHON太强大了!
#!/usr/bin/env python #encoding=utf-8 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 |