python学习笔记_week25

时间:2020-12-23 16:30:38
note
Day25
- 博客
- KindEditor
- beautifulsoup4对标签进行过滤
- 单例模式
- 事务操作
- from django.db import transaction
- 筛选条件
- 利用数据库内置函数实现筛选
作业:
- 示例
- 评论和点赞
pip3 install beautifulsoup4
- CMDB http://www.cnblogs.com/wupeiqi/articles/6192986.html
1. 资产自动收集
2. API (URL)
3. 可视化管理
1. 资产自动收集
- paramiko,ansible,fabric
通过API获取主机名,利用paramiko链接服务获取数据,解析成字典
优点:无依赖
缺点:慢
- saltstack
通过API获取主机名,利用salt api链接服务获取数据,解析成字典
优点:无依赖
缺点:有点慢
- pillar
- puppet
- 知识概要:
master
slave: certname 唯一标识)
slave: certname
配置文件:30分钟 master和slave进行一次连接
- report报表
配置文件
report: cmdb # 每30分钟交互时,回执行制定目录的下cmdb.rb文件中的process函数
- 自定义factor
- Agent
- 缺点:每台有agent
- 优点:快
v = subprocess.getoutput('hostname')
===================== 执行shell命令[网卡][硬盘]...,获取结果 ======================= - JSONP - 棘手的问题 跨域请求
import requests
request.get('http://www.baidu.com')
request.post('http://www.baidu.com') 由于浏览器具有同源策略(阻止Ajax请求,无法阻止<script src='...'></script>)
巧妙:
- 创建script标签
- src=远程地址
- 返回的数据必须是js格式
只能发GET请求 CORS ---http://www.cnblogs.com/wupeiqi/articles/5703697.html

python学习笔记_week25python学习笔记_week25

filter
 from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag
def filter_all(arg_dict,k):
"""
{% if arg_dict.article_type_id == 0 %}
<a class="active" href="/article-0-{{ arg_dict.category_id }}.html">全部</a>
{% else %}
<a href="/article-0-{{ arg_dict.category_id }}.html">全部</a>
{% endif %}
:return:
"""
if k == 'article_type_id':
n1 = arg_dict['article_type_id']
n2 = arg_dict['category_id']
if n1 == 0:
ret = '<a class="active" href="/article-0-%s.html">全部</a>' % n2
else:
ret = '<a href="/article-0-%s.html">全部</a>' % n2
else:
n1 = arg_dict['category_id']
n2 = arg_dict['article_type_id']
if n1 == 0:
ret = '<a class="active" href="/article-%s-0.html">全部</a>' % n2
else:
ret = '<a href="/article-%s-0.html">全部</a>' % n2
return mark_safe(ret)
@register.simple_tag
def filter_article_type(article_type_list,arg_dict):
"""
{% for row in article_type_list %}
{% if row.id == arg_dict.article_type_id %} {% else %}
<a href="/article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
{% endif %}
{% endfor %}
:return:
"""
ret = []
for row in article_type_list:
if row[0] == arg_dict['article_type_id']:
temp = '<a class="active" href="/article-%s-%s.html">%s</a>' %(row[0],arg_dict['category_id'],row[1],)
else:
temp = '<a href="/article-%s-%s.html">%s</a>' %(row[0],arg_dict['category_id'],row[1],)
ret.append(temp)
return mark_safe(''.join(ret))
models
 from django.db import models
# Create your models here.
class Category(models.Model):
caption = models.CharField(max_length=16)
# class ArticleType(models.Model):
# caption = models.CharField(max_length=16)
class Article(models.Model):
title = models.CharField(max_length=32)
content = models.CharField(max_length=255)
category = models.ForeignKey(Category,on_delete=models.CASCADE)
# article_type = models.ForeignKey(ArticleType)
type_choice = (
(1,'Python'),
(2,'OpenStack'),
(3,'Linux'),
)
article_type_id = models.IntegerField(choices=type_choice)
app01_views
 from django.shortcuts import render
from app01 import models
def article(request,*args,**kwargs):
print(kwargs)
# print(request.path_info) # 获取当前URL
# from django.urls import reverse
# # {'article_type_id': '0', 'category_id': '0'}
# url = reverse('article',kwargs={'article_type_id': '1', 'category_id': '0'})
# print(url)
# print(kwargs) # {'article_type_id': '0', 'category_id': '0'}
condition = {}
for k,v in kwargs.items():
kwargs[k] = int(v)
if v == '':
pass
else:
condition[k] = v
# article_type_list = models.ArticleType.objects.all()
article_type_list = models.Article.type_choice
category_list = models.Category.objects.all()
result = models.Article.objects.filter(**condition)
return render(
request,
'article.html',
{
'result': result,
'article_type_list': article_type_list,
'category_list': category_list,
'arg_dict': kwargs
}
)
app02_views
 from django.shortcuts import render
import requests
# pip3 install requests
# Create your views here.
def req(request):
response = requests.get('http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=101121301')
#print(response.content) # 字节
response.encoding = 'utf-8'
#print(response.text) # 字符串
return render(request, 'req.html',{'result': response.text})
urls
 from django.conf.urls import url
from django.contrib import admin
from app01 import views
from app02 import views as a2
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^req/', a2.req),
url(r'^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html',views.article,name='article'),
]
article
 {% load filter %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.condition a{
display: inline-block;
padding: 3px 5px;
border: 1px solid #dddddd;
margin: 5px ;
}
.condition a.active{
background-color: brown;
}
</style>
</head>
<body>
<h1>过滤条件</h1>
<div class="condition">
<div>
{% filter_all arg_dict 'article_type_id' %}
{% filter_article_type article_type_list arg_dict %}
</div> <div>
{% filter_all arg_dict 'category_id' %}
{% for row in category_list %}
{% if row.id == arg_dict.category_id %}
<a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
{% else %}
<a href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
{% endif %}
{% endfor %}
</div>
</div>
<h1>查询结果</h1>
<ul>
{% for row in result %}
<li>{{ row.id }}-{{ row.title }}</li>
{% endfor %}
</ul>
</body>
</html>
req
 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>后台获取的结果</h1>
{{ result }}
<h1>js直接获取结果</h1>
<input type="button" value="获取数据" onclick="getContent();" />
<div id="container"></div>
<script src="/static/jquery-1.8.2.js"></script>
<script>
function getContent(){
/*
var xhr = new XMLHttpRequest();
xhr.open('GET','http://wupeiqi.com:8001/jsonp.html?k1=v1&k2=v2');
xhr.onreadystatechange = function(){
console.log(xhr.responseText);
};
xhr.send();
*/
/*
var tag = document.createElement('script');
tag.src = 'http://www.jxntv.cn/data/jmd-jxtv2.html?callback=list&_=1454376870403';
document.head.appendChild(tag);
document.head.removeChild(tag);
*/
$.ajax({
url: 'http://www.jxntv.cn/data/jmd-jxtv2.html',
type: 'POST',
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'list'
})
}
function list(arg){
console.log(arg);
}
</script>
</body>
</html>

EdmureBlog  AutoClient