python学习笔记_week25

 

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
View Code

 

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

 EdmureBlog  AutoClient

posted on 2018-01-18 19:29  我很好u 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jyh-py-blog/p/8312681.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值