樱桃皮不暴露法

2024-09-21 05:44:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码如下:

import cherrypy as cp
import numpy as np
import pandas as pd
import os
import simplejson
import sys
import operator
global pagearray
global timearray
global state
timearray = np.load("buyertimearray.npy")
pagearray = np.load("buyerpagearray.npy")
state = "All"
MAX_WEBSITES = 10
DIR = os.path.abspath(".")

class Main(object):
    @cp.expose
    def index(self):
        return open(os.path.join(DIR+"/client/", 'index.html'))

    @cp.expose
    def refresh(self, starttime, endtime):
        global pagearray
        global timearray
        starttime = int(starttime)
        endtime = int(endtime)
        if state == "Buyers":
            pagearray = np.load("buyerpagearray.npy")
        elif state == "All":
            pagearray = np.load("allpagearray.npy")
        newpagearray =[]
        for i in np.argsort(timearray):
            newpagearray += [pagearray[i]]
        pagearray = newpagearray
        timearraysorted = np.sort(timearray)
        i=1
        startindex = 0
        endindex = 0
        while len(timearraysorted)-1 > i and starttime > int(timearraysorted[i]):
             startindex = i
             i+=1
        while len(timearraysorted)-1 > i and endtime > int(timearraysorted[i]):
             endindex = i
             i+=1
        pagearray = pagearray[startindex:endindex]

        startingpages = []
        if not pagearray:
            returnvaluelist = []
            weight = []
        else:
            for i in pagearray:
                if(i):
                    startingpages+=[i[0]]
            x = build_dict(startingpages)
            sorted_x = sorted(x.items(), key=operator.itemgetter(1), reverse=True)
            totalelements = len(pagearray)
            returnvaluelist = []
            weight = []
            for i in sorted_x:
                returnvaluelist += [i[0]]
                weight += [(i[1]/(totalelements*1.0))*100]
        return simplejson.dumps({"startingvalues":returnvaluelist, "weight":weight})

    @cp.expose
    def initialize(self):
        global pagearray
        global timearray
        global state
        pagearray = np.load("allpagearray.npy")
        timearray = np.load("alltimearray.npy")
        state = "All"
        firstwebsites = []
        firstwebsitepercentage = []
        totalvisitsfirst = np.sum(first)
        lastwebsites = []
        lastwebsitepercentage = []
        totalvisitslast = np.sum(last)
        for i in sorted_first[-49:]:
            firstwebsites += [vs.site[i]]
            firstwebsitepercentage += [(first[i]/totalvisitsfirst)*100]
        for i in sorted_last[-49:]:
            lastwebsites += [vs.site[i]]
            lastwebsitepercentage += [(last[i]/totalvisitsfirst)*100]
        return simplejson.dumps({"firstID":sorted_first[-49:].tolist(), "firstWebsites":firstwebsites, "firstPercentage":firstwebsitepercentage, "lastID":sorted_last[-49:].tolist(), "lastWebsites":lastwebsites, "lastPercentage":lastwebsitepercentage})

    @cp.expose
    def getPopLinks(self, siteId, direction):
        websiteArray = []
        siteId = float(siteId)
        if direction == "forward":
            rawcolumn = freq[:,siteId]
            sorted_column = np.argsort(freq[:,siteId])
        if direction == "backward":
            rawcolumn = freq[siteId,:]
            sorted_column = np.argsort(freq[siteId,:])
        websiteIDArray = sorted_column[-49:]
        sum_raw = np.sum(rawcolumn)
        percentages = []
        for i in websiteIDArray:
            percentages += [(rawcolumn[i]/sum_raw)*100]
            websiteArray += [vs.site[i]]
        return simplejson.dumps({"ids":websiteIDArray.tolist(), "websites":websiteArray, "percentages":percentages})

    @cp.expose
    def getUserPath(self, website):
        subpagearray = []
        possibilities = []
        global pagearray
        print pagearray
        for i in pagearray:
            try:
                print i[0]
                if website==i[0]:
                    subpagearray += [i[1:]]
                    possibilities+= [i[1]]
            except IndexError:
                print "IndexError!"
                pass
        x = build_dict(possibilities)
        sorted_x = sorted(x.items(), key=operator.itemgetter(1), reverse=True)
        pagearray = subpagearray
        totalelements = len(pagearray)
        returnvaluelist = []
        weight = []
        for i in sorted_x:
            returnvaluelist += [i[0]]
            weight += [(i[1]/(totalelements*1.0))*100]
        print returnvaluelist, weight
        return simplejson.dumps({"returnvaluelist":returnvaluelist, "weight":weight})

        @cp.expose
        def swap(self):
            global pagearray
            global timearray
            global state
            if state == "Buyers":
                pagearray = np.load("allpagearray.npy")
                timearray = np.load("alltimearray.npy")
                state = "All"
            else:
                pagearray = np.load("buyerpagearray.npy")
                timearray = np.load("buyertimearray.npy")
                state = "Buyers"


def build_dict(a_list=None):
    if a_list is None:
        a_list = []
    site_dict = {}
    for site in a_list:
        try:
            site_dict[site] = site_dict[site] + 1
        except KeyError:
            site_dict[site] = 1
    return site_dict


if __name__ == "__main__":
    vs = pd.read_pickle('violet_sitemap.pkl')
    filez = np.load('freq_last_first.txt')
    freq = filez['vs_array']
    last = filez['lp_array']
    first = filez['fp_array']
    sorted_first = np.argsort(first)
    sorted_last = np.argsort(last)
    cp.config.update({'server.socket_host': '0.0.0.0', 'server.socket_port':80})
    cp.quickstart(Main())

问题是当我通过jQuery在javascript中调用/getUserPath时,它返回一个404错误,说/getUserPath不存在,这是没有意义的,因为所有内容都是公开的,并指向实际的url

在www.serverurlhere.com/getUserPath?网站=http://otherurlhere.com/

显示有一个有效负载正在返回,并且有效负载正确。Javascript是100%正确的,因为我在它工作和不工作之间唯一更改的文件就是这个文件。为什么会出现404?这对我来说没有任何意义,因为我所做的只是在这个文件中添加一个方法,它就崩溃了(交换方法)。网站出现了,其他一切都正常,但是/getUserPath部分已经损坏。cherrypy服务器中方法的数量有限制吗?我对cherrypy一无所知,这很可能是cherrypy的一个局限。我在谷歌上搜了一下,找不到其他类似的东西。。。我只是不明白这里会发生什么。。。在

谢谢

F


Tags: inforifnpsiteloadglobalcp
1条回答
网友
1楼 · 发布于 2024-09-21 05:44:50

这个问题的答案是对方法签名使用从javascript发送的查询字符串参数使用相同的参数名。CherryPy关心参数名和接收的参数。在

从对这个问题的评论来看。在

I figured it out! Thanks for your help! Turns out it was a really stupid bug. Everything is working fine but i changed the name of the variable being passed into getUserPath to the name "website" from the name "input" and when the POST request was happening, cherrypy 404's when it receives something other than the exact variable name.

相关问题 更多 >

    热门问题