• html5-ajax-php-python-实现服务器文件夹搜索-前端显示


    1.网页前端

    Learn/index.php文件,前端搜索字符串。

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <script src="./js/jquery.min.js">script>
    <body>
    <div style="float: left"><input type="text" id="keyword" style="width: 300px;"/>div>
    <div style="float: left;margin-left: 10px;"><input type="button" id="SouSuo" value="搜索"/>div>
    <div style="clear: both"><a href="references/index.html">主页a>div>
    <div>
        <hr>
    div>
    <div style="float: none;width:500px; height:300px;clear: both;" id="showtext">div>
    
    
    body>
    html>
    
    <script type="text/javascript">
        $(document).ready(function (e) {
    
            $("#SouSuo").click(
                function () {
                    var keyword = $("#keyword").val();
                    var postdata = {"keyword": keyword};
    
                    console.log("关键词:" + postdata["keyword"]);
                    $.get("SearchContent.php", postdata,
                        function (data) {
                            $("#showtext").html(data);
                        });
                }
            );
        });
    script>
    
    • 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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    2.php端接收并调用python

    Learn/SearchContent.php文件实现调用python,并将结果返回。

    
    //这里两句话很重要,第一讲话告诉浏览器返回的数据是什么格式
    header("Content-Type: text/html;charset=utf-8");
    //告诉浏览器不要缓存数据
    header("Cache-Control: no-cache");
    
    $keyword = $_GET["keyword"];
    $cmd = shell_exec("python SearchHtmlFile.py $keyword");
    echo($cmd);
    ?>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.Python实现文件夹检索

    Learn/SearchHtmlFile.py实现文件夹检索:

    # coding=utf-8
    import os
    import json
    import re
    import sys
    
    # 读取文件
    def ReadFile(filename):
        #读取数据
        data = None;
        with open(filename, 'rb') as fr:
            data = fr.read();
    
        #编码转换
        try:
            strData = str(data, 'utf-8');
            return strData;
        except:
            try:
                strData = str(data, 'GBK');
                return strData;
            except Exception as e:
                try:
                    strData = str(data, 'latin1');
                    return strData;
                except Exception as e:
                    print(filename);
                    pass;
                pass;
        return None;
    
    # 查找内容
    def KeyWordExised(srcFullFilename,keyword):
        # 读取文件数据
        fileData = ReadFile(srcFullFilename);
        if fileData.find(keyword)>=0:
            return True;
        else:
            return False;
    
    # 搜索文件
    def SearchFolder(folderName,webstie,keyword):
        #结果
        fileListRlt=[];
    
        # 获取所有的头文件
        for root, dirs, files in os.walk(folderName):
            for shortFilename in files:
                # html
                fileInfo = os.path.splitext(shortFilename);
                if len(fileInfo) != 2:
                    continue;
                if fileInfo[1].lower() != ".html":
                    continue;
    
                # 读取文件数据
                srcFullFilename = os.path.join(root, shortFilename);
                if KeyWordExised(srcFullFilename,keyword):
                    fileListRlt.append(srcFullFilename);
    
        #格式化为html
        html="";
        for file in fileListRlt:
            sfilename = os.path.basename(file);
            webinfo=file.replace(folderName,webstie);
            webinfo=webinfo.replace("\\","/");
            atag='%s
    '
    %(webinfo,sfilename); html=html+atag; return html; para1 = sys.argv[1]; htmlStr=SearchFolder(r"C:\phpstudy_pro\WWW\Learn","http://119.23.229.158:100",para1); print(htmlStr);
    • 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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    4.php调用Shell问题

    PHP安全模式,网上说PHP安全模式会影响到shell_php()函数的执行。我查了一下,发现php.ini中安全模式只有sql。

    [SQL]
    sql.safe_mode=Off

    没有其余的安全模式。并且安全模式还会影响exec()函数的使用,我以前曾经使用过exec()函数,因此不是安全模式的问题。
    php.ini中disable_functions参数,百度时,有人说是php.ini配置文件中disable_funcitons=shell_exec()。禁用了shell_exec()函数。好吧,php.ini默认disable_funcitons= (null)。看来不是这里的缘由呢。程序执行目录问题,以后,我有检查了一下shell_exec()的执行代码,发现执行目录D:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe有问题。在第二个目录Program Files中有空格存在,以前考虑到不能含有中文的问题,因此安装wkhtmltopdf时,走的是D盘下的默认目录(原本是C盘,我改成D盘了)。既然不能走中文目录,是否是含有空格的目录也是不行的呢?因而我就把D:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe改成D:\wkhtmltopdf\bin\wkhtmltopdf.exe。而后修改了shell_exec()执行代码的执行目录。最后执行成功,生成了pdf文件。环境变量问题,此外把文件添加到环境变量中,能够不用带目录执行wkhtmltopdf程序。只不过,由于是测试wkhtmltopdf是否能执行含有html+css代码的文件生成带有css格式PDF文件,因此没有添加到环境变量中,但愿有小伙伴使用wkhtmltopdf时,能够安装wkhtmltopdf到D:\Program Files下,而后添加到环境变量内,用php的shell_exec()使用一下,看看可否成功。

    5.作者答疑

    如有疑问,敬请留言。

  • 相关阅读:
    四、Ribbon负载均衡
    [附源码]计算机毕业设计JAVAjsp医院挂号系统
    Java进阶(8)——抽象类与接口
    MongoDB - 事务支持
    MySQL事务的隔离级别
    Python画图3个小案例之“一起看流星雨”、“爱心跳动”、“烟花绚丽”
    Ubuntu12.04 内核树建立
    【IOS】oc中property属性值详解
    说说js中使用for in遍历数组存在的bug
    GitLab平台安装中经典安装语句含义解析
  • 原文地址:https://blog.csdn.net/m0_67316550/article/details/127133179