Cherrypy不暴露方法

Cherrypy Not exposing method

我的代码如下:

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

这个问题的答案是为方法签名使用相同的参数名称查询字符串参数从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.