-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetip.py
26 lines (25 loc) · 844 Bytes
/
getip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import re,urllib2
class GetPublicIp:
IP_ERROR = "110120119";
def getip(self):
try:
myip = self.visit("http://ddns.nat123.com")
except:
try:
myip = self.visit("http://1212.ip138.com/ic.asp")
except:
try:
myip = self.visit("http://ip.chinaz.com/getip.aspx")
except:
try:
myip = self.visit("http://www.ip.cn/")
except:
myip = self.IP_ERROR;
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 = GetPublicIp()
#print getmyip.getip()