如何将 Python 项目链接到 WSGI 文件?

发布于 2024-11-14 04:07:51 字数 1326 浏览 2 评论 0原文

我想将我的 Python 项目链接到 wsgi 文件。我正在使用 mod_wsgi

我希望我的 Python 项目位于 /var/www/myProject/start.py 中。

我已按如下方式配置 Apache:

<VirtualHost *:80>
    ServerName www.example.me
    ServerAlias example.me
    ServerAdmin [email protected]
    DocumentRoot /usr/local/www/documents
    LogLevel warn

    Alias /robots.txt /usr/local/www/documents/robots.txt
    Alias /favicon.png /usr/local/www/documents/favicon.png
    Alias /media/ /usr/local/www/documents/media/

    <Directory /usr/local/www/documents>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptAlias / /usr/local/www/wsgi-scripts/myApp.wsgi

    <Directory /usr/local/www/wsgi-scripts>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

到目前为止,这是我在 myApp.wsgi 文件中的内容:

import web

urls = (
    '/.*', 'hello',
)

class hello:
    def GET(self):
        return "Hello, world"

application = web.application(urls, globals()).wsgifunc()

为了链接位于 /var/ 中的项目,我需要做什么www/myProject/start.pymyApp.wsgi 调用?

I want to link my Python project to a wsgi file. I am using mod_wsgi.

I would like my Python project to be located in /var/www/myProject/start.py.

I've configured Apache as follows:

<VirtualHost *:80>
    ServerName www.example.me
    ServerAlias example.me
    ServerAdmin [email protected]
    DocumentRoot /usr/local/www/documents
    LogLevel warn

    Alias /robots.txt /usr/local/www/documents/robots.txt
    Alias /favicon.png /usr/local/www/documents/favicon.png
    Alias /media/ /usr/local/www/documents/media/

    <Directory /usr/local/www/documents>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptAlias / /usr/local/www/wsgi-scripts/myApp.wsgi

    <Directory /usr/local/www/wsgi-scripts>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

So far this is what I have in myApp.wsgi file:

import web

urls = (
    '/.*', 'hello',
)

class hello:
    def GET(self):
        return "Hello, world"

application = web.application(urls, globals()).wsgifunc()

What do I have to do in order to link my project which is located in /var/www/myProject/start.py to be called by myApp.wsgi?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱冒险 2024-11-21 04:07:51

将 WSGIScriptAlias 的最后一个参数替换为“/var/www/myProject/start.py”。

将目录指令中的引用更改为“/var/www/myProject”。换句话说,只需首先将配置设置为指向正确的位置即可。

Replace final argument to WSGIScriptAlias with '/var/www/myProject/start.py'.

Change reference in Directory directive to '/var/www/myProject'. In other words, just set the configuration to point to the correct location in the first place.

高冷爸爸 2024-11-21 04:07:51

看来您的 start.py 位于不同的目录中,您想从 wsgi.py 调用它。

  • 在这种情况下,您需要以某种方式告诉 wsgi.py 能够从不同的文件夹导入模块。 此线程对此有一些详细信息。

更好的方法是将 wsgi 文件放在与 start.py 相同的文件夹中,然后从那里导入并加载应用程序..类似于:

import start
start.load_application()

It seems that you have start.py in a different directory which you want to invoke from wsgi.py.

  • In this case, you need to somehow tell wsgi.py to be able to import a module from a different folder. This thread has some details on that.

A better way will be to have wsgi file in the same folder as the start.py, and just import and load the application from there.. something like:

import start
start.load_application()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文