cubieboard 发表于 2014-7-31 11:54:00

里程碑!家里的开发板能使用Dnspod的动态域名服务,稳定

最近,家里路由器的外网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 = '[email protected]'
      #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 = '[email protected]'
      #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([email protected])',
                                        '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']['value'].strip() != ip.strip():
                              self.api_call('record.modify',
                              {
                                        'domain_id' : domainId,
                                        'record_id' : rs['records']['id'],
                                        'sub_domain' : self._sub_domain,
                                        'record_type' : 'A',
                                        'record_line' : '默认',
                                        'value' : ip
                                        })
                        else:
                              print 'Success.'
               
if __name__ == '__main__':
      d = Dns();
      d.main()


截图:
http://forum.cubietech.com/data/attachment/forum/201303/21/104013jhsozava2sv07uga.jpg

原文作者:play4fun
原文链接:http://forum.cubietech.com/forum.php?mod=viewthread&tid=189

页: [1]
查看完整版本: 里程碑!家里的开发板能使用Dnspod的动态域名服务,稳定