首页 > temp > python入门教程 >
-
aliyun全站DCDN刷新--Django
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="/static/bootstrap/bootstrap.min.css">
<script src="/static/bootstrap/bootstrap.min.js"></script>
<script src="/static/js/jquery.min.js"></script>
</head>
<body>
<div class="alert alert-info" role="alert"><h3>久游DCDN刷新</h3></div>
<h2 style="margin-left:20px;margin-right: 20px"></h2>
<ul class="nav nav-tabs">
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<ul class="list-group">
<li class="list-group-item" id="huancun" role="presentation" class="active"><a href="/refresh/">刷新URL</a></li>
<li class="list-group-item" id="huancun" role="presentation" class="active"><a href="/redir/">目录刷新</a></li>
<li class="list-group-item" id="jilu" role="presentation"><a href="/result/">操作记录</a></li>
</ul>
</div>
{% block content %}
{% endblock %}
<div class="col-md-9">
<div class="jumbotron">
<div class="container">
{% block flush %}
{% endblock %}
</div>
</div>
</div>
</div>
</div>
{# <li id="huancun" role="presentation" class="active"><a href="/refresh/">刷新缓存</a></li>#}
{# <li id="redir" role="presentation" class="active"><a href="/redir/">目录刷新</a></li>#}
{# <li id="jilu" role="presentation"><a href="/result/">操作记录</a></li>#}
</ul>
</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 %}
{% extends "base.html" %} {% block content %} {% endblock %} {% block flush %} <form action="/refresh/" method="post" style="margin-top: 10px"> <div class="form-group"> <label>需要刷新的 URL </label><span><textarea rows="8" class="form-control" name="urlflush"></textarea></span> </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查看刷新结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
{% extends "base.html" %} {% block content %} < h4 >结果</ h4 > < table class="table table-bordered table-hover"> < thead ></ thead > < tbody > < tr class="success"> < 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
不用数据库模式,使用django中间件来验证账号密码登录
from django.utils.deprecation import MiddlewareMixin from django.shortcuts import HttpResponse,render user_dic={ 'kk': {'password': 'kk@123'}, 'bb': {'password': 'bb@123!'}, } class MyLogin(MiddlewareMixin): def process_request(self, request): if request.method == "POST": username = request.POST.get('username') password = request.POST.get('password') password = str(password) if username not in user_dic: return HttpResponse("没有这个用户") if password == user_dic[username]["password"]: return render(request, 'index.html') return HttpResponse("账号密码错误") return render(request, "login.html") def process_response(self,request,response): # print('from response1') return response