最近,家裏路由器的外網IP不穩定,老是不能訪問。鐵通的服務不好勁。
所以,http://dev.yangchun.so 暫停訪問。
在新浪微博聯繫我@JAVA搜索
http://weibo.com/richlinux
網站:http://www.yangchun.so
一直想着要把家裏的Cubieboard開發板搭一個網站,在外面能夠穩定訪問。
以前試了花生殼動態域名,很是不穩定。在外面沒法訪問。
直到昨天看到一篇文章。
linux下的動態域名解析 http://www.leadnt.com/2012/08/domains-dns-localdomains-linux/
若是服務器支持python,終端執行:
cd /home/
wget http://www.leadnt.com/tools/local_domains/local_domains_Python.zip
unzip local_domains_Python.zip
vi local_domains_Python.py
修改以下:
#Dnspod帳戶
_dnspod_user = 'test@example.com'
#Dnspod密碼
_dnspod_pwd = 'xxx'
#Dnspod主域名,注意:是你註冊的域名
_domain = 'yangchun.so'
#子域名,如www,若是要使用根域名,用@
_sub_domain = 'dev'
而後利用contab加入到計劃任務裏:
crontab -e
在定時任務里加入:
* 1 * * * python /home/local_domains_Python.py
這個是設置爲1小時運行一次。
或者直接運行:
root@localhost :/home# ./local_domains_Python.py
Success.
而後,個人子域名:http://dev.yangchun.so 就能夠穩定訪問了。
歡迎你們訪問,留個言什麼滴~
這個東西,跟 Cannikin 寫的文章是同樣的,他使用了3322.net的動態域名服務。
動態域名下的Cubieboard服務器http://itxp.3322.org/index.php/archives/33/
但實現過程不同。我這個使用python,是哪位做者本身寫的代碼,使用DNSPon服務。
Cannikin使用了lynx ,這是個文本瀏覽器。
lynx -mime_header -auth=申請的用戶名:密碼 http://members.3322.net/dyndns/update?system=dyndns&hostname=申請的免費動態域名
其實就是一行命令。
建議你們使用DNSpod,畢竟是本身的域名,能夠自由使用。
其實個人開發板,今天是用電小二開着,看電小二的5000毫安電量能撐多久。
local_domains_Python.py源代碼:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import urllib2,urllib,json
class Dns:
#Dnspod帳戶
_dnspod_user = 'test@example.com'
#Dnspod密碼
_dnspod_pwd = 'xxx'
#Dnspod主域名,注意:是你註冊的域名
_domain = 'yangchun.so'
#子域名,如www,若是要使用根域名,用@
_sub_domain = 'app'
def getMyIp(self):
try:
u = urllib2.urlopen('http://www.leadnt.com/tools/ip.php')
return u.read()
except HTTPError as e:
print e.read()
return None;
def api_call(self,api,data):
try:
api = 'https://dnsapi.cn/' + api
data['login_email'] = self._dnspod_user
data['login_password'] = self._dnspod_pwd
data['format'] ='json'
data['lang'] = 'cn'
data['error_on_empty'] = 'no'
data = urllib.urlencode(data)
req = urllib2.Request(api,data,
headers = {
'UserAgent' : 'LocalDomains/1.0.0(roy@leadnt.com)',
'Content-Type':'application/x-www-form-urlencoded;text/html; charset=utf8',
})
res = urllib2.urlopen(req)
html = res.read()
results = json.loads(html)
return results
except Exception as e:
print e
def main(self):
ip = self.getMyIp()
dinfo = self.api_call('domain.info',{'domain' : self._domain})
domainId = dinfo['domain']['id']
rs = self.api_call('record.list',
{
'domain_id': domainId,
'offset' :'0',
'length' : '1',
'sub_domain' : self._sub_domain
})
if rs['info']['record_total'] == 0:
self.api_call('record.create',
{
'domain_id' : domainId,
'sub_domain' : self._sub_domain,
'record_type' : 'A',
'record_line' : '默認',
'value' : ip,
'ttl' : '3600'
})
print 'Success.'
else:
if rs['records'][0]['value'].strip() != ip.strip():
self.api_call('record.modify',
{
'domain_id' : domainId,
'record_id' : rs['records'][0]['id'],
'sub_domain' : self._sub_domain,
'record_type' : 'A',
'record_line' : '默認',
'value' : ip
})
else:
print 'Success.'
if __name__ == '__main__':
d = Dns();
d.main()
截圖:php
原文做者:play4funhtml
原文連接:http://forum.cubietech.com/forum.php?mod=viewthread&tid=189python