因使用路由器工具上的花生壳检测IP不准,估计是ISP运营商封了花生壳的检测网址,写了个脚本,自动检测当前IP并绑定域名
</pre><pre name="code" class="python">
</pre><pre name="code" class="python">
</pre><pre name="code" class="python">
</pre><pre name="code" class="python"># coding=utf-8
'''
Created on 2015年1月5日
@author: Osborn
'''
import urllib.request
import re
import time
from curses.ascii import NUL
def catch(url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept':'text/html;q=0.9,*/*;q=0.8',
'Accept-Charset':'utf-8;q=0.7,*;q=0.3',
'Accept-Encoding':'gzip',
'Connection':'close',
'Referer': ""
}
opener = urllib.request.build_opener()
opener.addheaders = headers
data = opener.open(url).read()
return data
def posttooray(ip):
hostname = '****'
url = "http://ddns.oray.com/ph/update?hostname=%s&myip=%s" % (hostname, ip)
username = '**'
password = '##'
base64string = base64.b64encode(('%s:%s' % (username, password)).encode(encoding="utf-8"))
# print(base64string)
headers = {'User-Agent':'Oray',
'Authorization':'Basic '+ <span style="font-family: Arial, Helvetica, sans-serif;">base64string</span>
}
opener = urllib.request.build_opener()
opener.addheaders = headers
data = opener.open(url).read()
print(data)
def getIP():
ret = catch("http://1111.ip138.com/ic.asp").decode("gb2312")
pattern = re.compile(".*您的IP是:\[([\d \.]*)\].*")
match = pattern.findall(ret, re.U | re.M)
if match:
return (match[0])
return NUL
if __name__ == '__main__':
while True:
posttooray(getIP())
time.sleep(300)