Python CGI,FieldStorage和javascript数组

时间:2017-06-28 13:10:27

标签: javascript python arrays cgi

我搜索和搜索,我确信有一个简单的答案。然而,解决方案逃脱了我。我有javascript,它接受所有

  • 元素,并将id属性放入一个数组,然后将其发送到apache http服务器。然后,使用python,应该检索数据,将数据制作成python列表,然后迭代。

    我的问题是我似乎无法获得可迭代的列表。以下是FieldStorage的输出:

    FieldStorage(None, None, [MiniFieldStorage('data', '[["NWS-IDP-PROD-KEEPALIVE-30678","NWS-IDP-PROD-2426831","NWS-IDP-PROD-2426829-2276816","NWS-IDP-PROD-2426827-2276815","NWS-IDP-PROD-2426823-2276812","NWS-IDP-PROD-2426824-2276814","NWS-IDP-PROD-2426823-2276811","NWS-IDP-PROD-2426823-2276813","NWS-IDP-PROD-2426822-2276809","NWS-IDP-PROD-2426822-2276810","NWS-IDP-PROD-2426822-2276808","NWS-IDP-PROD-2426822-2276807","NWS-IDP-PROD-2426821","NWS-IDP-PROD-2426819-2276806","NWS-IDP-PROD-2426818-2276805","NWS-IDP-PROD-2426817-2276804","NWS-IDP-PROD-2426815-2276801","NWS-IDP-PROD-2426816-2276803","NWS-IDP-PROD-2426813-2276800","NWS-IDP-PROD-2426807-2276797","NWS-IDP-PROD-2426806-2276796","NWS-IDP-PROD-2426805-2276795","NWS-IDP-PROD-2426803-2276793","NWS-IDP-PROD-2426802-2276792","NWS-IDP-PROD-2426800-2276791","NWS-IDP-PROD-2426796-2276787","NWS-IDP-PROD-2426797-2276789","NWS-IDP-PROD-2426793-2276785","NWS-IDP-PROD-2426792-2276784","NWS-IDP-PROD-2426787-2276779","NWS-IDP-PROD-2426791-2276783","NWS-IDP-PROD-2426783-2276775","NWS-IDP-PROD-2426785-2276777","NWS-IDP-PROD-2426784-2276776","NWS-IDP-PROD-2426789-2276781","NWS-IDP-PROD-2426788-2276780","NWS-IDP-PROD-2426786-2276778","NWS-IDP-PROD-2426790-2276782","NWS-IDP-PROD-2426781-2276773","NWS-IDP-PROD-2426779-2276771","NWS-IDP-PROD-2426778-2276770","NWS-IDP-PROD-2426777-2276769","NWS-IDP-PROD-2426774-2276766","NWS-IDP-PROD-2426768-2276762","NWS-IDP-PROD-2426764-2276760","NWS-IDP-PROD-2426758-2276751","NWS-IDP-PROD-2426757-2276750","NWS-IDP-PROD-2426756-2276749","NWS-IDP-PROD-2426754-2276747","NWS-IDP-PROD-2426755-2276748","NWS-IDP-PROD-2426753-2276746","NWS-IDP-PROD-2426752-2276745","NWS-IDP-PROD-2426750-2276743","NWS-IDP-PROD-2426749-2276742","NWS-IDP-PROD-2426748-2276741","NWS-IDP-PROD-2426747-2276740","NWS-IDP-PROD-2426743-2276736","NWS-IDP-PROD-2426744-2276737","NWS-IDP-PROD-2426746-2276739","NWS-IDP-PROD-2426745-2276738","NWS-IDP-PROD-2426538-2276561","NWS-IDP-PROD-2426539-2276562","NWS-IDP-PROD-2426536-2276559","NWS-IDP-PROD-2426535-2276558","NWS-IDP-PROD-2426502-2276526","NWS-IDP-PROD-2426501-2276525","NWS-IDP-PROD-2426495-2276519","NWS-IDP-PROD-2426490-2276514","NWS-IDP-PROD-2426494-2276518","NWS-IDP-PROD-2426493-2276517","NWS-IDP-PROD-2426492-2276516","NWS-IDP-PROD-2426487-2276512","NWS-IDP-PROD-2426484-2276509","NWS-IDP-PROD-2426483-2276508","NWS-IDP-PROD-2426479-2276504","NWS-IDP-PROD-2426477-2276503","NWS-IDP-PROD-2426475-2276502","NWS-IDP-PROD-2426458-2276486","NWS-IDP-PROD-2426453-2276482","NWS-IDP-PROD-2426452-2276481","NWS-IDP-PROD-2426450-2276480","NWS-IDP-PROD-2426442-2276474","NWS-IDP-PROD-2426437-2276470","NWS-IDP-PROD-2426438-2276471","NWS-IDP-PROD-2426433-2276466","NWS-IDP-PROD-2426432-2276465","NWS-IDP-PROD-2426434-2276467","NWS-IDP-PROD-2426430-2276463","NWS-IDP-PROD-2426431-2276464","NWS-IDP-PROD-2426203-2276345","NWS-IDP-PROD-2426204-2276346","NWS-IDP-PROD-2426102-2276282","NWS-IDP-PROD-2426052-2276244","NWS-IDP-PROD-2426019-2276228","NWS-IDP-PROD-2425905-2276157","NWS-IDP-PROD-2425891-2276153","NWS-IDP-PROD-2425874-2276146","NWS-IDP-PROD-2425851-2276136","NWS-IDP-PROD-2425801-2276114","NWS-IDP-PROD-2425748-2276070","NWS-IDP-PROD-2425747-2276069","NWS-IDP-PROD-2425746-2276068","NWS-IDP-PROD-2425745-2276067","NWS-IDP-PROD-2425744-2276066","NWS-IDP-PROD-2425743-2276065","NWS-IDP-PROD-2425681-2276015","NWS-IDP-PROD-2424738-2275345","NWS-IDP-PROD-2421587-2272709","NWS-IDP-PROD-2419354-2270782"]]')])
    

    和javascript代码:

    mkTableArray: function() {
        var myTableArray = [];
        $("#alerts").each(function() {
            var arrayOfThisRow = [];
            var tableData = $(this).find('li');
            if (tableData.length > 0) {
                tableData.each(function() {
                    arrayOfThisRow.push($(this).attr("id"));
                });
                myTableArray.push(arrayOfThisRow);
            }
        });
        var myJson = JSON.stringify(myTableArray);
        return myJson;
    }
    
    function fetchNewAlerts() {
        data = Alert.mkTableArray();
    
        $.post( "fetchNew.py", {data}, function(data) {
            $("#alerts").prepend(data);
            $(".new").hide().fadeIn(1000);
        });
    }
    

    蟒蛇:

    data = cgi.FieldStorage()
    mfs = data.getvalue("data")
    print(mfs)
    for line in mfs:
      print(line)
    

    Python将整个事物视为一个列表项。因此,对于x in y循环,仅打印出您在上面看到的内容。它将它视为列表中的所有项目实际上都是单个项目。而且我不知道如何,或者我还没弄清楚如何分别获取每个NWS-IDP-PROD-2426826项目,以便我可以测试每次迭代。我试过.split(),但无济于事。任何帮助将不胜感激。谢谢。

  • 1 个答案:

    答案 0 :(得分:0)

    我一定是大脑失灵了。通过这样做我能够实现我的目标:

    SCHEDULER = Rufus::Scheduler.new(lockfile: "/tmp/.rufus-scheduler.lock")