Python 在共享web主机上将Flask部署为CGI

Python 在共享web主机上将Flask部署为CGI,python,.htaccess,flask,cgi,Python,.htaccess,Flask,Cgi,我有一个小烧瓶应用程序,我希望部署。我在bigrock.com上购买了域名和托管服务。 我编写了一个小的模拟应用程序来测试我的Flask应用程序是否可以作为CGi工作。 我使用了以下配置: 文件:/home/USERNAME/public\u html/cgi-bin/cgi.cgi #!/usr/bin/env python from wsgiref.handlers import CGIHandler from myapp import app import os import cgi

我有一个小烧瓶应用程序,我希望部署。我在bigrock.com上购买了域名和托管服务。 我编写了一个小的模拟应用程序来测试我的Flask应用程序是否可以作为CGi工作。 我使用了以下配置:

文件:
/home/USERNAME/public\u html/cgi-bin/cgi.cgi

#!/usr/bin/env python  

from wsgiref.handlers import CGIHandler
from myapp import app
import os
import cgitb; cgitb.enable()

os.environ['SERVER_NAME'] = '127.0.0.1'
os.environ['SERVER_PORT'] = '5000'
os.environ['REQUEST_METHOD'] = 'GET'
os.environ['PATH_INFO'] = ""
CGIHandler().run(app)
文件:
/home/USERNAME/public\u html/cgi-bin/myapp.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "This is Hello World!\n"

if __name__ == "__main__":
    app.run()
文件:
/home/USERNAME/public\u html/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/USERNAME/public_html/cgi-bin/cgi.cgi/$1 [L]
使用ssh会话运行cgi.cgi作为
/cgi.cgi

#!/usr/bin/env python  

from wsgiref.handlers import CGIHandler
from myapp import app
import os
import cgitb; cgitb.enable()

os.environ['SERVER_NAME'] = '127.0.0.1'
os.environ['SERVER_PORT'] = '5000'
os.environ['REQUEST_METHOD'] = 'GET'
os.environ['PATH_INFO'] = ""
CGIHandler().run(app)
给出以下输出:

Status: 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 21

This is Hello World!
同时运行myapp.py作为

python myapp.py

激活开发服务器:

Running on http://127.0.0.1:5000/
现在,不管我何时访问我的网站:

www.mysite.com

www.mysite.com/cgi.cgi

www.mysite.com/cgi-bin/cgi.cgi

我得到一个500内部服务器错误

当我使用Python hello world cgi脚本时,它工作得非常好:

#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""
#/usr/bin/env python
打印“内容类型:文本/html”
打印
打印“”\
你好,世界!
"""

我知道类似的问题一次又一次地被提出来,但没有一个得到正确的回答,或者答案似乎不起作用。

经过多次调整,我终于让它起作用了。 谢谢各位

我认为这就成功了:
我将所有文件上载到/scgi-bin并编辑了.htaccess以反映相同的情况。

尝试使用IP而不是cgi.cgi中的127.0.0.1。请检查“否”,使用IP也没有帮助。我只是根据Flask的cgi建议进行了设置。如果您还发布了500响应中的异常日志,这将更有帮助。我想它应该在httpd错误日志中。请添加您详细的答案,以便帮助后代。