【问题标题】:Cherrypy and Pyramid integrationCherrypy 和 Pyramid 集成
【发布时间】: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 中。

标签: pyramid wsgi cherrypy


【解决方案1】:

我不确定我是否发现了所有问题,但我确实看到了一些错误。首先,您的 url 在您的 ajax 调用中关闭。接下来在您的 views.py 中,您使用 jsonp 而不是 json 作为渲染器。此外,您对@view_config 使用了“route_name”而不是“route”(ajax 调用)。最后,您返回了一个字符串。我把它改成了字典。

如果您不以直截了当的方式设置项目结构,那么 Pyramid 可能会很棘手。我学会了艰难的方式:)

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/data/views.py

from pyramid.view import view_config

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

现在查看您的整体文件结构后,它看起来不像一个标准的金字塔应用程序。你有很多事情要做,对我来说它看起来像是过度编程。有很多重复的代码。也许你这样做是有原因的,但我不知道。

我在下面包含了一个金字塔开始 git repo。我构建它是为了帮助人们开始在 Openshift 上放置金字塔项目。我认为您的金字塔项目应该遵循相同的大纲。无需深层文件夹。

您要特别注意的文件是“app.py.disabled”文件。不要介意残疾人的部分。有两种方法可以启动 Openshift 金字塔应用程序,这个 git repo 使用的是 wsgi.py 文件。您可以将两者切换。

无论如何,在 app.py.disabled 文件中,您可以看到我使用 wsgi 服务器(simple、waitress 和cherrypy)设置金字塔应用程序的所有不同方式。只需取消注释/注释掉您想要的代码即可。

我认为您正在混合使用樱桃框架和金字塔框架。只需使用cherrypy 的wsgi 服务器。不要做任何cherrypy配置。我最后一次听到的是 Cherrypy 正在将他们的 wsgi 服务器从他们的框架中分离出来。至少我看了一年。

您可以尝试使用女服务员。非常好和简单,适用于所有平台。

Openshift-Pyramidstarter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-05
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2016-09-27
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多