2017-11-30 19 views
0

私は例えば、_cp_dispatchメソッド内CherryPyにさらさクラスのメソッドを呼び出すためにしようとしている:_cp_dispatch内のクラスメソッドを呼び出せませんか?

class ABC(object): 

@cherrypy.expose 
def post(self): 
    return "POSTING" 

def _cp_dispatch(self, vpath): 
    if len(vpath) == 1: 
     return self.post() 
    return vpath 

「ポスト」の方法は、私は、インデックス(自己)に変更しない限り呼ばれる、と教えてくれません。しかし、自己を返す_cp_dispatch。それ以外の場合、私はURL 8080:/ ABCを入力すると、404エラーが表示されるpath to /ABC/ not found ここで問題は何ですか?

cherrypyを使用して単一のURLパスに対して複数のメソッドを作成することはできませんか?

答えて

0

は、getメソッドとpostメソッドを保持する2番目のクラスを作成したばかりです。完全なコードは次のようになります。

import cherrypy 
import random 
import string 

class data(object): 
def __init__(self): 
    self.catalogue = catalogue() 

def _cp_dispatch(self, vpath): 
    if len(vpath) == 1: 
     return self.catalogue 

    return vpath 

class catalogue(object): 
exposed = True 

@cherrypy.tools.accept(media='text/plain') 
def GET(self): 
    return cherrypy.session['mystring'] 

def POST(self, length=8): 
    some_string = ''.join(random.sample(string.hexdigits, int(length))) 
    cherrypy.session['mystring'] = some_string 
    return some_string 

if __name__ == '__main__': 
conf = { 
    '/': { 
     'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 
     'tools.sessions.on': True, 
     'tools.response_headers.on': True, 
     'tools.response_headers.headers': [('Content-Type', 'text/plain')] 
    } 
} 
cherrypy.quickstart(data(),'/',conf) 
関連する問題