巡风xunfeng代码研究---核心模块深入分析--搜索和搜索结果

下面的是核心文件:

 

1) search.html
2) main.html
3) View.py


说明: 本项目使用的是jquery js脚本方式,请注意,在写脚本的时候请在页面后面添加jquery的相关js文件进去到html页面尾部


<script src="static/js/jquery.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/js/detect.js"></script>
<script src="static/js/fastclick.js"></script>
<script src="static/js/jquery.slimscroll.js"></script>
<script src="static/js/jquery.blockUI.js"></script>
<script src="static/js/waves.js"></script>
<script src="static/js/wow.min.js"></script>
<script src="static/js/jquery.nicescroll.js"></script>
<script src="static/js/jquery.scrollTo.min.js"></script>
<script src="static/plugin/jquery.poshytip.min.js"></script>
<script>
    $('#tips').poshytip({
        className: 'tip-twitter',
        showTimeout: 1,
        alignTo: 'target',
        alignX: 'center',
        alignY: 'bottom',
        offsetX: -100,
        offsetY: 30,
        allowTipHover: false,
        fade: false,
        slide: false,
        content: "<p>查询方法:</p> \
                        <p>1.按端口: port:端口号 eg. port : 22</p>\
                        <p>2.按banner: banner:banner内容关键词 eg. banner : ftp</p>\
                        <p>3.按ip(支持c段,b段模糊查询): ip:ip地址  eg. ip : 192.168.1.1/ip : 192.168.1.</p>\
                        <p>4.按服务名: server:服务名  eg. server : iis</p>\
                        <p>5.按标题: title:标题内容关键词 eg. title : xxx管理系统</p>\
                        <p>6.按服务类型标签: tag:服务类型 eg. tag : apache</p>\
                        <p>7.按主机名: hostname:主机名 eg. hostname : server001</p>\
                        <p>8.全局模糊: all:查询内容  eg. all : tongcheng</p>\
                        <p>9.多条件: 条件1:内容1;条件2:内容2  eg. ip:192.168.1.1;port:22</p>",
    });
</script>
<script src="static/js/jquery.core.js"></script>
<script src="static/js/jquery.app.js"></script>
</body>
</html>

 

然后 在这里面指定搜索的入口,然后通过/filter  url跳转到后台的search页面。即显示搜索页面。

                    <li class="has-submenu">
                        <a href="/filter"><i class="zmdi zmdi-view-dashboard"></i> <span> 搜索 </span> </a>
                        <ul class="submenu">
                            <li><a href="/filter">条件筛选</a></li>
                            <li><a href="/">自定义</a></li>
                        </ul>
                    </li>

 

然后在html里有一个form,填写查询条件后就可进行查询

 
 <div class="wrapper-page searchbar" style="margin: 14% auto">
                    <div class="m-t-40 card-box">
                        <div class="text-center">
                            <a href="#" class="logo"><span>巡风</span></a>
                        </div>
                        <div class="panel-body">
                            <form method="get" action="/" role="form" class="text-center">
                                <div class="form-group">
                                    <input type="text" class="form-control"
                                           placeholder="Example:  ip: 192.168.1.1; port: 22"
                                           style="color: #797979;" id="filter" name="q">

                                    <button type="submit"
                                            class="btn btn-inverse btn-rounded w-md waves-effect waves-light m-b-5"
                                            style="margin-top: 20px">搜索
                                    </button>
                                    <i class="zmdi zmdi-help-outline zmdi-hc-2x" style="position: relative"
                                       id="tips"></i>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

 

其中的 action="/" 说明指向的是: 

 

即在form点击了确定搜索后,通过提交q变量给的后台,

 

将会跳转到/视图, 也就是上图的Main方法执行

 

# 搜索结果页
@app.route('/')
@logincheck
def Main():
    q = request.args.get('q', '')
    page = int(request.args.get('page', '1'))
    plugin = Mongo.coll['Plugin'].find()  # 插件列表
    plugin_type = plugin.distinct('type')  # 插件类型列表
    if q:  # 基于搜索条件显示结果
        result = q.strip().split(';')
        query = querylogic(result)
        cursor = Mongo.coll['Info'].find(query).sort('time', -1).limit(page_size).skip((page - 1) * page_size)
        return render_template('main.html', item=cursor, plugin=plugin, itemcount=cursor.count(),
                               plugin_type=plugin_type)
    else:  # 自定义,无任何结果,用户手工添加
        return render_template('main.html', item=[], plugin=plugin, itemcount=0, plugin_type=[])

 

从代码上可以看出: 

后台主要执行两个命令: 

 (1) Mongo.coll['Plugin'].find() # 插件列表

        self.conn = MongoClient(self.host, self.port)
        self.coll = self.conn[self.database]
        self.coll.authenticate(username, password)

 可见是指一个 MongoClient 实例,然后是切换到database,进行find()查询,查询所有清单 [优化建议]

plugin_type = plugin.distinct('type')  # 插件类型列表

  从find()查询出来的游标里面获取名字是 type的数据清单

  find()后还可以是.sort()  .limit() .skip()

 (2) result = q.strip().split(';')          query = querylogic(result)

  此案例是:将IP:端口  多个主机分组: 如将字符窜"10.1.0.38:3306;10.1.0.39:3306"分为['10.1.0.38:3306','10.1.0.38:3306'],然后作为参数进行逻辑分析query = querylogic(result).

  querylogic详情请看:  这里

  (3) 

  

以下的是 flask的 render_template源代码: 

 

def render_template(template_name_or_list, **context):
    """Renders a template from the template folder with the given
    context.

    :param template_name_or_list: the name of the template to be
                                  rendered, or an iterable with template names
                                  the first one existing will be rendered
    :param context: the variables that should be available in the
                    context of the template.
    """
    ctx = _app_ctx_stack.top
    ctx.app.update_template_context(context)
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
                   context, ctx.app)

 

以下的是flask的request.args的简单介绍:

request.args.get  此方法是 FLask获取一条参数的值
如:
from flask import Flask,request

app = Flask(__name__)

@app.route('/')
def hello():
    return {"param":request.args.get('abc')}

此时访问http://127.0.0.1:5000/?abc=hello将得到{"param": "hello"}

另外补充: Flask如何获取全部的参数的值

@app.route('/')
def hello():
    return request.args.items().__str__()

访问http://127.0.0.1:5000/?abc=hello&xyz=world&ab=hellohello
则得到[('abc', u'hello'), ('xyz', u'world'), ('ab', u'hellohello')]

 

python的strip和  split

strip(rm)---当rm为空的时候,默认是说删除前后空格

           ---当rm有值的时候,是rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉

 

split  --  将字符串分割成“字符”,保存在一个列表中.默认不带参数为空格分割.

       --- 还可以带参数根据实际需求进行分割

   ----  还可以带上数字参数,表示“切几刀”如: 

 

posted @ 2017-03-27 15:02  八月的男人  阅读(1198)  评论(0编辑  收藏  举报