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查看刷新结果
{% 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
验证账号密码
aliyun全站DCDN刷新--Django的更多相关文章
- Django
一.Django 简介 Django 是一个由 Python 写成的开放源代码的 Web 应用框架.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是 CMS(内容管理系统) ...
- django 补充篇
from验证 django中的Form一般有两种功能: 输入html-----------不能你自己写一些标签,而帮你自动生成 验证用户输入-------将用户验证信息保存起来,可以传到前端 # !/ ...
- Django实现表单验证、CSRF、cookie和session、缓存、数据库多表操作(双下划綫)
通常验证用户输入是否合法的话,是前端js和后端共同验证的,这是因为前端js是可以被禁用的,假如被禁用了,那就没法用js实现验证合法与否了,也就是即使用户输入的不合法,但是也没提示,用户也不知道怎么输入 ...
- Django框架全面讲解
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- Python Django缓存,信号,序列化,文件上传,Ajax登录和csrf_token验证
本节内容 models操作 Django的缓存 请求方式 序列化 Form 配合Ajax实现登录认证 上传文件 Ajax csrf_token验证方式 1 models操作 单表查询: curd(增 ...
- django面试题
1. 对Django的认识? #1.Django是走大而全的方向,它最出名的是其全自动化的管理后台:只需要使用起ORM,做简单的对象定义,它就能自动生成数据库结构.以及全功能的管理后台. #2.D ...
- Django 的认识,面试题
Django 的认识,面试题 1. 对Django的认识? #1.Django是走大而全的方向,它最出名的是其全自动化的管理后台:只需要使用起ORM,做简单的对象定义,它就能自动生成数据库结构.以及全 ...
- Python 19 Django 详解
本节概要 Django详解 前言 有一部分原因是,确实djando的课程有点多:并且,最近又在研究利用python做数据分析时间上耽误了.所以楼主讲所有的课程全部重新观看了一遍,再来撰写博客,其实说起 ...
- django面试八
1. 对Django的认识? #1.Django是走大而全的方向,它最出名的是其全自动化的管理后台:只需要使用起ORM,做简单的对象定义,它就能自动生成数据库结构.以及全功能的管理后台. #2.Dja ...
- Pyhton-Web框架之【Django】
一.什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的 ...
随机推荐
- 记录-JavaScript常规加密技术
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 当今Web开发中,数据安全是一个至关重要的问题,为了确保数据的安全性,我们需要使用加密技术.JavaScript作为一种客户端编程语言,可 ...
- 记录--一道js笔试题, 刷新了我对map方法函数的认知
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 背景 昨天在看一道笔试题的时候本以为很简单,但是结果不是我想象的那样,直接上笔试题. const array = new Array(5) ...
- 性能测试系列:高可用测试linux常用命令
一 linux常用 df –h 看磁盘 du –h –max-depth=1 查看当前目录下,各个文件夹大小 ls –lht 查看当前目录下,各个文件大小 top –H –p pid 看进程下线程的资 ...
- 15 分钟带你感受 CSS :has() 选择器的强大
最近看到了许多关于 :has() 选择器的知识点,在此总结下来. MDN 对 :has() 选择器 的解释是这样的: CSS 函数式伪类 :has() 表示一个元素,如果作为参数传递的任何相对选择 ...
- vscode中vue代码提示插件
由来 基于(Vue 3 Snippets,Vue VSCode Snippets)插件集成而来,因为这俩插件目前没有集成最新vue代码片段,且集成内容相对较少,于是这个插件就诞生了 插件提示跟vue写 ...
- C# SM2
Cipher using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle. ...
- verilog设计知识集合(2)
verilog设计知识集合(2) 1.阻塞与非阻塞 阻塞赋值是存在先后关系的,非阻塞是不存在先后关系的.一般而言,阻塞用于组合逻辑,非阻塞用于时序逻辑(不一定).阻塞的执行时逐步赋值,非阻塞是同步赋值 ...
- 实现一个简单的echarts柱状图PythonFlask
bar.html 1 <!DOCTYPE html> 2 <html style="height: 100%"> 3 <head> 4 < ...
- C++简单实现vector
向量 向量是序列容器,表示可以更改大小的数组. 就像数组一样,向量对其元素使用连续的存储位置,这意味着也可以使用指向其元素的常规指针上的偏移量来访问其元素,并且与数组一样高效.但与数组不同的是,它们的 ...
- 鸿蒙HarmonyOS实战-ArkUI组件(TextInput/TextArea)
一.TextInput/TextArea TextInput和TextArea组件通常用于收集用户输入的文本数据. TextInput组件通常用于单行文本的输入,它允许用户通过一个光标来输入文字,并支 ...