2016-03-23 37 views
1

我有一個使用mod_wsgi在apache下運行的金字塔應用程序。 我打算從apache遷移到cherrypy。 我可以用cherrypy加載現有web應用程序的靜態頁面。但是,對於任何AJAX請求,我收到資源未找到(404)錯誤。 任何線索?櫻桃和金字塔集成

由於

30-MAR-2016

這裏是代碼結構

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() 
+0

你只是使用CherryPy的WSGI服務器爲您金字塔的應用程序?這是我所做的唯一金字塔/櫻桃整合。 –

+0

這是一個例子,我如何在Openshift上設置我的金字塔應用程序。有一些在App.py文件中使用女服務員和cherrypy的設置示例。 Hthhttp://stackoverflow.com/questions/27264103/how-to-create-app-using-pyramid-into-openshift/27324518#27324518 –

回答

1

我不知道如果我能抓到的一切,但我沒有看到一對夫婦的錯誤。首先,你的網址在你的ajax調用中關閉。接下來在您的views.py中,您使用jsonp而不是json作爲您的渲染器。此外,您使用「route_name」而不是「路由」(ajax調用)作爲@view_config。最後,你正在返回一個字符串。我把它改成了字典。

如果您不直接設置項目結構,金字塔可能會非常棘手。我學到了艱辛的道路:) 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: "getdata", 
    success: function (data) 
    { 
    console.log("LED On"); 
    }, 
    error: function() 
    { 
     console.error("ERROR"); 
    } 
    }); 
}); 
</script> 
</body> 
</html> 

文件mproject/MYDATA /數據/ views.py

from pyramid.view import view_config 

@view_config(name='get_data', renderer='json') 
def get_data(request): 
    return {'firstName' : 'John'} 

現在在看後你整體的文件結構,它看起來不像一個標準的金字塔應用程序。你有很多事情要做,而且看起來像是在編程給我。有很多重複的代碼。也許你這樣做是有原因的,但我不知道。

我包括金字塔下面的開始git回購。我建立它幫助人們開始在Openshift上放置金字塔項目。我認爲你的金字塔項目應該遵循同樣的大綱。不需要深層文件夾。

您要特別關注的文件是「app.py.disabled」文件。不介意殘疾人部分。有兩種方式來啓動一個Openshift金字塔應用程序,這個git倉庫使用wsgi.py文件。你可以切換這兩個。

無論如何,在app.py.disabled文件中,您可以看到我使用wsgi服務器(simple,waitress和cherrypy)設置金字塔應用程序的所有不同方式。只需取消註釋/註釋掉你想要的代碼。

我想你是在混合櫻桃框架和金字塔框架。只需使用cherrypy的wsgi服務器即可。不要做任何cherrypy配置。我聽到的最後一句話是,Cherrypy將他們的wsgi服務器從他們的框架中分離出來。自從我看了至少已經有一年了。

你可能只是嘗試使用女服務員。非常好,簡單,適用於所有平臺。

Openshift-Pyramidstarter