2016-03-23 1 views
1

J'ai une application pyramidale fonctionnant sous apache en utilisant mod_wsgi. Je prévois de migrer d'apache à cherrypy. Je suis capable de charger la page statique de l'application web existante avec cherrypy. Cependant, pour toute requête AJAX, j'obtiens une erreur de ressource introuvable (404). Des indices?Intégration Cherrypy et Pyramide

Merci

30-Mar-2016

Voici la structure du code

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) 

Contenu de 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> 

Fichier 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() 

Fichier myproject/views.py

from pyramid.view import view_config 

Fichier myproject/mydata/__init__.py

import data 

Fichier 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() 

Fichier 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'}" 

Fichier 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') 

Fichier 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

Êtes-vous simplement en utilisant le serveur wsgi de cherrypy pour votre application Pyramide? C'est la seule intégration pyramide/cherrypie que j'ai faite. –

+0

Voici un exemple comment je configurer mes applications Pyramid sur Openshift. Il existe des exemples d'installation de waitress et cherrypy dans le fichier App.py. Hth.http: //stackoverflow.com/questions/27264103/how-to-create-app-using-pyramid-into-openshift/27324518#27324518 –

Répondre

1

Je ne sais pas si je pris tout, mais je l'ai vu quelques bugs. Tout d'abord, votre URL était désactivée dans votre appel ajax. Ensuite, dans votre views.py, vous utilisiez jsonp et non json comme moteur de rendu. En outre, vous utilisiez "route_name" au lieu de "route" (appels ajax) pour @view_config. Enfin, vous retourniez une chaîne. Je l'ai changé en dict.

Pyramide peut être difficile si vous ne configurez pas votre structure de projet d'une manière directe. J'ai appris à la dure :)

Contenu de 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> 

Fichier mProject/mydata/data/views.py

from pyramid.view import view_config 

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

maintenant après avoir regardé vous structure de fichiers globale, il ne ressemble pas à une application pyramide standard. Vous avez beaucoup de choses qui se passent et il me semble qu'il y a trop de programmation pour moi. Il y a beaucoup de code en double. Peut-être que vous faites cela pour une raison mais je ne sais pas.

J'ai inclus ci-dessous un repo git de début de pyramide. Je l'ai construit pour aider les gens à commencer à mettre en place des projets pyramidaux sur Openshift. Je pense que votre projet pyramidal devrait suivre le même schéma.Pas besoin de dossiers profonds.

Le fichier sur lequel vous souhaitez accorder une attention particulière est le fichier "app.py.disabled". Ne vous occupez pas de la partie handicapée. Il y a deux façons de démarrer une application de pyramide Openshift et ce repo git utilise le fichier wsgi.py. Vous pouvez simplement changer les deux.

Quoi qu'il en soit, dans le fichier app.py.disabled, vous pouvez voir toutes les différentes façons que j'ai utilisées pour configurer l'application pyramide en utilisant les serveurs wsgi (simple, serveuse et cherrypy). Il suffit de décommenter/commenter le code que vous voulez. Je pense que vous mélangez le cadre cherrypy et la charpente pyramidale. Utilisez simplement le serveur wsgi de cherrypy. Ne faites pas de configuration Cherrypy. Le dernier que j'ai entendu était que Cherrypy séparait leur serveur wsgi de leur cadre. Cela fait au moins un an que j'ai regardé.

Vous pouvez essayer d'utiliser Waitress. Très bon et simple et fonctionne sur toutes les plateformes.

Openshift-Pyramidstarter