【发布时间】:2016-07-10 12:48:29
【问题描述】:
我有一个使用 mod_wsgi 在 apache 下运行的金字塔应用程序。 我打算从apache迁移到cherrypy。 我可以使用cherrypy 加载现有Web 应用程序的静态页面。但是对于任何 AJAX 请求,我都会收到资源未找到 (404) 错误。 有什么线索吗??
谢谢
2016 年 3 月 30 日
这是代码结构
MyProject
|
cherry_wsgi.py (creates wsgi app object)
cherry_server.py (starts cherrypy server using app object from cherry_wsgi.py)
development.ini
myproject
|
__init__.py (Scans sub-folders recursively)
views.py
mydata
|
__init__.py
data
|
__init__.py (Added route for getdata)
views.py (implementation of getdata)
|
myclient
|
index.html (AJAX query)
myclient/index.html 的内容
<html>
<head>
<meta charset="utf-8">
<title>HOME UI</title>
</head>
<body>
<button id="submit">Give it now!</button>
<script src="./jquery-2.1.3.min.js"></script>
<script>$("#submit").on('click', function()
{
$.ajax(
{
type: "GET",
async: false,
url: "../myproject/data/getdata",
success: function (data)
{
console.log("LED On" );
},
error: function ()
{
console.error("ERROR");
}
});
});</script></body></html>
文件myproject/__init__.py
from pyramid.config import Configurator
from pyramid.renderers import JSONP
import os
import logging
def includeme(config):
""" If include function exists, write this space.
"""
pass
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""
config = Configurator(settings=settings)
config.add_renderer('jsonp', JSONP(param_name='callback'))
config.include(includeme)
directory = "/home/redmine/Downloads/MyProject/myproject/mydata/"
for root,dir,files in os.walk(directory):
if root == directory:# Walk will return all sublevels.
for dirs in dir: #This is a tuple so we need to parse it
config.include('myproject.mydata.' + str(dirs), route_prefix='/' + str(dirs))
config.add_static_view('static', 'prototype', cache_max_age=3600)
config.scan()
return config.make_wsgi_app()
文件myproject/views.py
from pyramid.view import view_config
文件myproject/mydata/__init__.py
import data
文件mproject/mydata/data/__init__.py
from pyramid.config import Configurator
def includeme(config):
config.add_route('get_data', 'getdata', xhr=True)
def main(global_config, **settings):
print 'hello'
config = Configurator(settings=settings)
config.include(includeme, route_prefix='/data')
config.add_static_view('static', 'prototype', cache_max_age=3600)
config.scan('data')
return config.make_wsgi_app()
文件mproject/mydata/data/views.py
from pyramid.view import view_config
import json
@view_config(route_name='get_data', xhr=True, renderer='jsonp')
def get_data(request):
return "{'firstName' : 'John'}"
文件cherry_wsgi.py
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.paster import get_app
config = Configurator()
app = get_app('development.ini', 'main')
文件cherry_server.py
from cherry_wsgi import app
import cherrypy
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': '/home/redmine/Downloads/MyProject/'
},
'/myclient': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './myclient'
}
}
if __name__ == '__main__':
cherrypy.tree.mount(app, "/", conf)
cherrypy.server.unsubscribe()
server = cherrypy._cpserver.Server()
server.socket_host = "0.0.0.0"
server.socket_port = 9090
server.thread_pool = 30
server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
【问题讨论】:
-
您只是在 Pyramid 应用程序中使用cherrypy 的 wsgi 服务器吗?这是我做过的唯一 Pyramid/cherrypy 集成。
-
是的。我正在使用cherrypy的wsgi服务器进行金字塔。我关注了这篇文章digitalocean.com/community/tutorials/…(注意我还没有使用过nginx,但计划在不久的将来使用它。目前只有cherrypy。)
-
以下是我如何在 Openshift 上设置 Pyramid 应用程序的示例。 App.py 文件中有使用waitress 和cherrypy 的设置示例。 Hth.stackoverflow.com/questions/27264103/…
-
如果您发布您的 ajax 代码以及您的金字塔 view.py 代码,我会很好。我们可以去那里。
-
在您的问题中发布并格式化您的代码,而不是在 cmets 中。