我在尝试在cherryPy服务器中配置全局404错误时遇到问题。
这是我的代码。
import cherrypy
import os,os.path
import view.costume_functions as cf
import model.database as db
import temp.template as tm
class Maple_syrup_project(object):
@cherrypy.expose
def index(self):
query = db.get_racks()
estado = {}
for rack in query:
ip_address = rack[1]
key = rack[0]
estado[key] = cf.estado_actal(ip_address)
return tm.rack_all(query,estado)
@cherrypy.expose
def configurar(self,**post):
comando = cf.comando(post)
rack_id = post['rack_id']
ip_address = db.get_ip(rack_id);
respuesta = cf.connection(ip_address,comando)
return comando
return 'configurando...'+comando+'....'+respuesta
def error_page_404(status, message, traceback, version):
return ('Oppps Error')
cherrypy.root = Maple_syrup_project()
if __name__=='__main__':
configurations = {
'/':{
'tools.staticdir.root':os.path.abspath(os.getcwd())
},
'/static':{
'tools.staticdir.on':True,
'tools.staticdir.dir':'./static'
}
}
cherrypy.config.update({'server.socket_port':9999,
'server.socket_host':'0.0.0.0',
'error_page.404': error_page_404,
})
cherrypy.tree.mount(Maple_syrup_project(),
'/control_de_retornos',
configurations)
cherrypy.engine.start()
cherrypy.engine.block()
如果我将浏览器指向ht ..:// ....:9999 / control_de_retornos / xxxxx 我得到了预期的服装错误'Ooops Error'。 如果我将我的浏览器指向ht ..:// ....:9999 /或ht ..:// ....:9999 / xxxxx 我有一个空白页面,其中包含状态代码:标题中没有找到404,但这不是我的服装错误。
关于发生了什么的任何想法? 谢谢。
答案 0 :(得分:0)
尝试这样的事情......
import cherrypy
import os,os.path
class Maple_syrup_project(object):
class control_de_retornos(object):
@cherrypy.expose
def index(self):
return 'hi'
@cherrypy.expose
def configurar(self,**post):
return 'configurando...'
def error_page_404(status, message, traceback, version):
return ('Oppps Custom Error')
cherrypy.root = Maple_syrup_project()
if __name__=='__main__':
configurations = {
'/':{
'tools.staticdir.root':os.path.abspath(os.getcwd()),
'server.socket_port':9999,
'server.socket_host':'0.0.0.0',
'error_page.404': error_page_404,
},
'/static':{
'tools.staticdir.on':True,
'tools.staticdir.dir':'./static'
}
}
cherrypy.tree.mount(Maple_syrup_project(),
'/',
configurations)
cherrypy.engine.start()
cherrypy.engine.block()
error_page_404需要与Maple_syrup_project分开,而不是“class control_de_retornos(object):”的一部分。现在,当我走到你的路径的根,我得到“Oppps自定义错误”。 希望这有帮助!
答案 1 :(得分:0)
感谢Andrew Kloos
我解决了它添加另一个安装在'/'上的类并处理错误
import cherrypy
import os,os.path
import view.costume_functions as cf
import model.database as db
import temp.template as tm
class Maple_syrup_project(object):
@cherrypy.expose
def index(self):
racks = db.get_racks()
rack_collection = {}
bson = {}
for rack in racks:
ip_address = rack[1]
try:
estado = cf.estado_actal(ip_address)
except:
return ('Opsss..... Error....... Verificar que los racks esten encendidos y bien conectados ala red')
nombre = db.get_optos_name(ip_address)
opto_collection = cf.create_bson(estado,nombre,rack)
rack_collection['rack_'+str(rack[0])] = opto_collection
bson['racks'] = rack_collection
return tm.rack_all(bson)
@cherrypy.expose
def configurar(self,**post):
comando = cf.comando(post)
rack_id = post['rack_id']
ip_address = db.get_ip(rack_id);
respuesta = cf.connection(ip_address,comando)
return 'configurando...'+comando+'....'+respuesta
class Index(object):
pass
def error_page_404(status, message, traceback, version):
return ('Oppps Error')
if __name__=='__main__':
configurations = {
'/':{
'tools.staticdir.root':os.path.abspath(os.getcwd())
},
'/static':{
'tools.staticdir.on':True,
'tools.staticdir.dir':'./static'
}
}
cherrypy.config.update({'server.socket_port':9998,
'server.socket_host':'0.0.0.0',
'error_page.404': error_page_404
})
cherrypy.tree.mount(Maple_syrup_project(),'/control_de_retornos',configurations)
cherrypy.tree.mount(Index(),'/',configurations)
cherrypy.engine.start()
cherrypy.engine.block()