主程序 几个函数调用阿里云接口 flask 前端交互 加入 http 认证 很 low 的代码 只作为测试使用
#!/usr/bin/env python
#encoding: utf-8
[aliyundnszip](https://b3logfile.com/pipe/up/up/up/1652e09ecb824a1b8ea000a3202393c5.zip)
from flask import Flask, render_template, request, flash
from flask_httpauth import HTTPBasicAuth
from aliyunsdkalidns.request.v20150109 import AddDomainRequest, AddDomainRecordRequest, DescribeDomainRecordsRequest,DescribeRecordLogsRequest
from aliyunsdkcore import client
import redis
import json
# import urllib, urllib2
app = Flask(__name__)
app.secret_key = 'kjsensldfneapk'
auth = HTTPBasicAuth() #http
users = [
{'username': 'admin', 'password': 'admin'},
]
r = redis.StrictRedis(host='172.16.10.133', port=31350, db=0)
# a={'Action': 'ADD', 'ActionTimestamp': 1515035872000L, 'Message': '\xe6\x96\xb0\xe5\xa2\x9e\xe8\xa7\xa3\xe6\x9e\x90\xe8\xae\xb0\xe5\xbd\x95 A\xe8\xae\xb0\xe5\xbd\x95 blog \xe9\xbb\x98\xe8\xae\xa4 61.128.128.68 ( TTL: 600) ', 'ActionTime': '2018-01-04T11:17Z', 'ClientIp': '14.105.38.140'}, {'Action': 'DEL', 'ActionTimestamp': 1514960338000L, 'Message': '\xe5\x88\xa0\xe9\x99\xa4\xe8\xa7\xa3\xe6\x9e\x90\xe8\xae\xb0\xe5\xbd\x95 A\xe8\xae\xb0\xe5\xbd\x95 pay \xe9\xbb\x98\xe8\xae\xa4 172.16.10.25 ( TTL: 600)', 'ActionTime': '2018-01-03T14:18Z', 'ClientIp': '14.105.38.140'}
# b=str(a)
# print b
# r.set('A',b)
# r.set('A','{'Action': 'ADD', 'ActionTimestamp': 1515035872000L, 'Message': '\xe6\x96\xb0\xe5\xa2\x9e\xe8\xa7\xa3\xe6\x9e\x90\xe8\xae\xb0\xe5\xbd\x95 A\xe8\xae\xb0\xe5\xbd\x95 blog \xe9\xbb\x98\xe8\xae\xa4 61.128.128.68 ( TTL: 600) ', 'ActionTime': '2018-01-04T11:17Z', 'ClientIp': '14.105.38.140'}, {'Action': 'DEL', 'ActionTimestamp': 1514960338000L, 'Message': '\xe5\x88\xa0\xe9\x99\xa4\xe8\xa7\xa3\xe6\x9e\x90\xe8\xae\xb0\xe5\xbd\x95 A\xe8\xae\xb0\xe5\xbd\x95 pay \xe9\xbb\x98\xe8\xae\xa4 172.16.10.25 ( TTL: 600)', 'ActionTime': '2018-01-03T14:18Z', 'ClientIp': '14.105.38.140'}')
# print r.get('db')
#阿里云相关认证
# access_key_id = "xxxx"
access_key_id = "xxxx"
# access_Key_secret = "xx"
access_Key_secret = "xxxxx"
# Version = "2015-01-09"
# #主域名
# rc_domain = "youngblog.cc"
# #请填写你的解析记录
# rc_rr = 'pay'
# #需要添加的记录
# rc_add = 'blog'
# #客服端ip
# rc_ip= "61.128.128.68"
# #请填写返还内容格式,json,xml
rc_format = 'json'
#检查域名是否存在
# def check_records(dns_domain):
# # clt = client.AcsClient(access_key_id, access_Key_secret, 'cn-hangzhou')
# clt = client.AcsClient(access_key_id, access_Key_secret)
# request = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
# request.set_DomainName(dns_domain)
# request.set_accept_format(rc_format)
# result = clt.do_action_with_exception(request)
# return result
def add_dns(dns_domain,dns_add,dns_value,dns_type,dns_format):
#阿里云dns 调用请参考 https://github.com/aliyun/aliyun-openapi-python-sdk/tree/master/aliyun-python-sdk-alidns/aliyunsdkalidns/request/v20150109
clt = client.AcsClient(access_key_id, access_Key_secret)
# request = AddDomainRequest.AddDomainRequest()#方法调用错误
request = AddDomainRecordRequest.AddDomainRecordRequest()
request.set_DomainName(dns_domain)
request.set_RR(dns_add)
request.set_Value(dns_value)
request.set_Type(dns_type)
request.set_accept_format(dns_format)
result = clt.do_action_with_exception(request)
# print result
return result
###获取操作记录
def get_domain(dns_domain,dns_format):
clt = client.AcsClient(access_key_id, access_Key_secret)
request = DescribeRecordLogsRequest.DescribeRecordLogsRequest()
request.set_DomainName(dns_domain)
request.set_accept_format(dns_format)
result = clt.do_action_with_exception(request)
#eval 转换json不能识别中文,这里使用json.loads进行转换
return json.loads(result)['RecordLogs']['RecordLog'][0:10]
#转换后中文乱码 等待从redis中读取后再处理
# return eval(result)['RecordLogs']['RecordLog'][0:10]
# print get_domain(rc_domain,rc_format)
def getinfoRdis():
for k,v in enumerate(get_domain(rc_domain,rc_format)):
str_v=str(v)
r.set(k,str_v)
return eval(r.get(0))['Message']
##http 认证
@auth.get_password
def get_password(username):
for user in users:
if user['username'] == username:
return user['password']
return None
# print check_records(rc_domain)
# print add_dns(rc_domain,rc_add,rc_ip,"A",rc_format)
# def post_url(url, data):
# req = urllib2.Request(url)
# data = urllib.urlencode(data)
# opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
# resp = opener.open(req, data)
# return resp.read()
# # flask 应用部分
@app.route('/', methods=["GET", "POST"])
@auth.login_required
def index():
if request.method == 'POST':
domain = request.form['domain']
adddomain = request.form['adddomain']
ip_value = request.form['ip_value']
dnstype = request.form['dnstype']
if adddomain == "cahsier" or adddomain == "image" or adddomain == "pay" or adddomain == "merch" or adddomain == "www" or adddomain == "":
flash(u'过滤敏感信息')
return render_template('index.html')
c = add_dns(domain,adddomain,ip_value,dnstype,'json')
print c
flash(u'更新域名完成请稍后查看')
print "相关地址:domain,adddomain,ip_value,dnstype",domain,adddomain,ip_value,dnstype
if request.method == 'GET':
# flash(get_domain(rc_domain,rc_format))
flash(getinfoRdis())
# print add_dns(rc_domain,rc_add,rc_ip,"A",rc_format)
return render_template('index.html')
# elif request.method == 'GET':
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081)
前端代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>添加域名解析</title>
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- Bootstrap 3.3.6 -->
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="/static/css/AdminLTE.min.css">
<!-- iCheck -->
<link rel="stylesheet" href="/static/css/blue.css">
</head>
<body class="hold-transition login-page">
<div class="login-box">
<div class="login-logo">
<a >添加域名解析</a>
</div>
<!-- /.login-logo -->
<div class="login-box-body">
<form action="/" method="post">
<div class="form-group has-feedback">
<input type="text" name="domain" class="form-control" placeholder="域名" value="域名" readonly>
{# <input type="text" name="domain" class="form-control" placeholder="youngblog.cc" value="youngblog.cc" readonly>#}
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="text" name="adddomain" class="form-control" placeholder="主机记录/如blog">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="text" name="ip_value" class="form-control" value="61.128.158.218">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="text" name="dnstype" class="form-control" placeholder="A" value="A" readonly>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
pay,merch,www.等域名不能解析,请填写正确的IP地址,存在相同记录会报错
<div class="row">
<div class="col-xs-8">
</div>
<!-- /.col -->
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">确定</button>
</div>
<!-- /.col -->
</div>
</form>
<div class="social-auth-links text-center">
</div>
<!-- /.social-auth-links -->
</div>
<!-- /.login-box-body -->
{% block custom_message %}
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="select-bar">
<ul style="margin:0 0 0 10px;padding:5px 10px;">
{% for category, message in messages %}
<li style="font-size:15px;">{{ message | safe }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}
{% endblock %}
</div>
目录结构 css fonts 这些就直接在网上下载就行
├── run.py
├── static
│ ├── css
│ │ ├── AdminLTE.css
│ │ ├── AdminLTE.min.css
│ │ ├── blue.css
│ │ ├── bootstrap.css
│ │ ├── bootstrap.css.map
│ │ ├── bootstrap.min.css
│ │ ├── bootstrap.min.css.map
│ │ ├── bootstrap-theme.css
│ │ ├── bootstrap-theme.css.map
│ │ ├── bootstrap-theme.min.css
│ │ ├── bootstrap-theme.min.css.map
│ │ ├── dataTables.bootstrap.css
│ │ ├── font-awesome.min.css
│ │ └── ionicons.min.css
│ └── fonts
│ ├── glyphicons-halflings-regular.eot
│ ├── glyphicons-halflings-regular.svg
│ ├── glyphicons-halflings-regular.ttf
│ ├── glyphicons-halflings-regular.woff
│ └── glyphicons-halflings-regular.woff2
├── templates
│ └── index.html
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于