1.撰寫原因:
由于登錄到阿里云DCDN,需要登錄加打開各種頁面,導致推送一次感覺非常麻煩,所以撰寫(網上以有很多可以借鑒)
2.基礎環境
# 所需模塊 pip install aliyun-python-sdk-core-v3 pip install aliyun-python-sdk-dcdn pip install django==1.11.11
3.Django對應檔案修改
修改 settings.py
# 添加可訪問的主機 ALLOWED_HOSTS = ['*'] # 注釋csrf # 'django.middleware.csrf.CsrfViewMiddleware', # 注釋DATABASES # 修改時區 TIME_ZONE = 'Asia/Shanghai' # 添加資源目錄 STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), # 放置:bootstrap.min.css,bootstrap.min.js,jquery.min.js )
修改urls.py
from cdn import views urlpatterns = [ url(r'^refresh/', views.refresh), url(r'^result/', views.result), url(r'^redir/', views.redir) ]
修改views.py
from django.shortcuts import render import json from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkdcdn.request.v20180115.RefreshDcdnObjectCachesRequest import RefreshDcdnObjectCachesRequest from aliyunsdkdcdn.request.v20180115.DescribeDcdnRefreshTasksRequest import DescribeDcdnRefreshTasksRequest client = AcsClient('<accessKeyId>', '<accessSecret>', 'ap-southeast-1') # 重繪URL def refresh(req): if req.method == "POST": request = RefreshDcdnObjectCachesRequest() request.set_accept_format('json') msg = req.POST.get("urlflush", None) request.set_ObjectPath(msg) request.set_ObjectType("file") response = client.do_action_with_exception(request) print(str(response, encoding='utf-8')) return render(req, "index.html", {}) # 重繪目錄 def redir(req): if req.method == "POST": request = RefreshDcdnObjectCachesRequest() request.set_accept_format('json') msg = req.POST.get("dirflush", None) request.set_ObjectPath(msg) request.set_ObjectType("Directory") response = client.do_action_with_exception(request) print(str(response, encoding='utf-8')) return render(req, "dir_ref.html", {}) # 獲取重繪結果 def result(req): request = DescribeDcdnRefreshTasksRequest() request.set_accept_format('json') response = client.do_action_with_exception(request) dict_str = json.loads(str(response, encoding='utf-8')) dic_data = dict_str["Tasks"]["Task"] return render(req, "result.html", {'dic_data': dic_data})
4.在 templates 檔案夾下添加 html 檔案
添加base.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>9you</title> <link rel="stylesheet" href=https://www.cnblogs.com/yangmeichong/p/"/static/bootstrap/bootstrap.min.css"> <script src=https://www.cnblogs.com/yangmeichong/p/"/static/bootstrap/bootstrap.min.js"></script> <script src=https://www.cnblogs.com/yangmeichong/p/"/static/js/jquery.min.js"></script> </head> <body> <h2 style="margin-left:20px;margin-right: 20px">DCDN重繪</h2> <ul class="nav nav-tabs"> <li id="huancun" role="presentation" class="active"><a href=https://www.cnblogs.com/yangmeichong/p/"/refresh/">重繪快取</a></li> <li id="redir" role="presentation" class="active"><a href=https://www.cnblogs.com/yangmeichong/p/"/redir/">目錄重繪</a></li> <li id="jilu" role="presentation"><a href=https://www.cnblogs.com/yangmeichong/p/"/result/">操作記錄</a></li> </ul> {% block content %} {% endblock %} </body> </html>
添加index.html重繪URL
{% extends "base.html" %}
{% block content %}
<form action="/refresh/" method="post" style="margin-top: 10px">
<div class="form-group">
<label>需要重繪的 URL </label>
<input type="text" class="form-control" placeholder="URL" name="urlflush" style="width: 60%">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
{% endblock %}
添加dir_ref.html重繪目錄
{% extends "base.html" %}
{% block content %}
<form action="/redir/" method="post" style="margin-top: 10px">
<div class="form-group">
<label>需要重繪的目錄鏈接 </label>
<input type="text" class="form-control" placeholder="DIR" name="dirflush" style="width: 60%">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
{% endblock %}
添加result.html查看重繪結果
{% extends "base.html" %}
{% block content %}
<h4>結果</h4>
<table >
<thead></thead>
<tbody>
<tr >
<td>操作內容</td>
<td>操作時間</td>
<td>狀態</td>
<td>進度</td>
</tr>
{% for dic in dic_data %}
<tr>
<td>{{ dic.ObjectPath }}</td>
<td>{{ dic.CreationTime }}</td>
<td>{{ dic.ObjectType }}</td>
<td>{{ dic.Status }}</td>
<td>{{ dic.Process }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
$(function () {
$('#jilu').addClass('active');
$('#huancun').removeClass('active')
})
</script>
{% endblock %}
5.訪問鏈接
http://IP:8000/refresh

參考與轉載:
阿里云:https://help.aliyun.com/document_detail/130620.html?spm=a2c4g.11186623.6.726.53dcb427zWsP2v
https://my.oschina.net/u/4365358/blog/4093467
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/228401.html
標籤:Python
下一篇:11 Serializer組件
