使用 CGI 部署 Flask 应用程序

发布于 2024-12-03 06:06:06 字数 696 浏览 1 评论 0原文

我使用 Flask 框架编写了一个小型应用程序。我尝试使用 cgi 来托管它。根据文档,我创建了一个包含以下内容的 .cgi 文件:

#!/usr/bin/python
from wsgiref.handlers import CGIHandler
from yourapplication import app

CGIHandler().run(app)

运行该文件会导致以下错误:

...

文件“/usr/lib/pymodules/python2.7/werkzeug/routing.py”,第 1075 行,bind_to_environ wsgi_server_name = environ.get('HTTP_HOST',environ['SERVER_NAME'])
关键错误:“SERVER_NAME”
状态:500 内部服务器错误
内容类型:文本/纯文本
内容长度:59

在我的应用程序中,我设置了:

app.config['SERVER_NAME'] = 'localhost:5000'

当我使用 Flask 开发服务器运行应用程序时,它运行得很好。 正如你所知,我对这些东西非常陌生,并且我搜索了其他有类似错误的人,但没有运气。感谢所有帮助。

I have written a small application using the Flask framework. I try to host this using cgi. Following the documentation I created a .cgi file with the following content:

#!/usr/bin/python
from wsgiref.handlers import CGIHandler
from yourapplication import app

CGIHandler().run(app)

Running the file results in following error:

...

File "/usr/lib/pymodules/python2.7/werkzeug/routing.py", line 1075, in bind_to_environ
wsgi_server_name = environ.get('HTTP_HOST', environ['SERVER_NAME'])
KeyError: 'SERVER_NAME'
Status: 500 Internal Server Error
Content-Type: text/plain
Content-Length: 59

In my application I have set:

app.config['SERVER_NAME'] = 'localhost:5000'

When I run the application with the Flask development server it works perfectly well.
As you can tell I'm very new to this stuff and I have search for others with similar errors but with no luck. All help is appreciated.

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

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

发布评论

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

评论(2

菊凝晚露 2024-12-10 06:06:06

我将尝试展示我所做的事情,并且它在 Godaddy 共享主机帐户中工作:

在 MYSITE 文件夹中的 cgi-bin 文件夹中,我添加了以下 cgi 文件:

#!/home/USERNAME/.local/bin/python3
from wsgiref.handlers import CGIHandler

from sys import path
path.insert(0, '/home/USERNAME/public_html/MYSITE/')
from __init__ import app

class ProxyFix(object):
   def __init__(self, app):
       self.app = app

   def __call__(self, environ, start_response):
       environ['SERVER_NAME'] = ""
       environ['SERVER_PORT'] = "80"
       environ['REQUEST_METHOD'] = "GET"
       environ['SCRIPT_NAME'] = ""
       environ['QUERY_STRING'] = ""
       environ['SERVER_PROTOCOL'] = "HTTP/1.1"
       return self.app(environ, start_response)

if __name__ == '__main__':
    app.wsgi_app = ProxyFix(app.wsgi_app)
    CGIHandler().run(app)

如您所见 init MYSITE 文件夹中的文件有 Flask 应用程序。

最重要的是权限设置正确。我将此文件夹权限设置为 755 以及“/home/USERNAME/.local/bin/python3”文件夹!请记住,系统需要此权限才能打开 Flask。

要打开 cgi,我在 MYSITE 文件夹中有以下 .htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /home/USERNAME/public_html/MYSITE/cgi-bin/application.cgi/$1 [L]

因此,当有人进入您的页面时,它将呈现 cgi 文件。

I will try to show what I've done and it is working in Godaddy sharing host account:

In the cgi-bin folder in MYSITE folder, I added the following cgi file:

#!/home/USERNAME/.local/bin/python3
from wsgiref.handlers import CGIHandler

from sys import path
path.insert(0, '/home/USERNAME/public_html/MYSITE/')
from __init__ import app

class ProxyFix(object):
   def __init__(self, app):
       self.app = app

   def __call__(self, environ, start_response):
       environ['SERVER_NAME'] = ""
       environ['SERVER_PORT'] = "80"
       environ['REQUEST_METHOD'] = "GET"
       environ['SCRIPT_NAME'] = ""
       environ['QUERY_STRING'] = ""
       environ['SERVER_PROTOCOL'] = "HTTP/1.1"
       return self.app(environ, start_response)

if __name__ == '__main__':
    app.wsgi_app = ProxyFix(app.wsgi_app)
    CGIHandler().run(app)

As you can see the init file in the MYSITE folder have the flask app.

The most important thing is to set the permissions right. I setted 755 to this folder permission AS WELL AS to "/home/USERNAME/.local/bin/python3" folder!! Remember that the system needs this permission to open flask.

To open the cgi I have the following .htaccess file in MYSITE folder:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /home/USERNAME/public_html/MYSITE/cgi-bin/application.cgi/$1 [L]

So it will render the cgi file when someone enters your page.

半窗疏影 2024-12-10 06:06:06

为了完整起见,这是作为上述评论之后的答案发布的。

如上所述,cgi 脚本应该由某个服务器执行。以下是来自 CGI 1.1 RFC 的摘要:

通用网关接口(CGI)是一个简单的接口,用于运行
信息服务器下的外部程序、软件或网关
一种独立于平台的方式。目前支持的信息
服务器是 HTTP 服务器。

对于环境变量(缺少并触发错误),请参阅 RFC 中的 sectuib 4.1。

This is posted as an answer following the comments above for the sake of completeness.

As discussed above, cgi scripts should execute by some server. Here's the abstract from CGI 1.1 RFC:

The Common Gateway Interface (CGI) is a simple interface for running
external programs, software or gateways under an information server in
a platform-independent manner. Currently, the supported information
servers are HTTP servers.

For the environment variables (which were missing and triggered the error) see sectuib 4.1 in the RFC.

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