Python 如何使用wsgiref CGIHandler摆脱运行Flask应用程序的额外cgi bin url组件?
我的共享cpanel托管计划不直接支持wsgi应用程序。因此,我必须使用wsgiref CGIHandler解决方案,如下所述: 这一切都可以正常工作并产生预期的结果,但在URL中总是有额外的内容:“/cgi-bin/index.cgi/”,python应用程序似乎会自动添加这些内容(以匹配cgi处理程序调用时检测到的内容) 例如,我希望它是myhost.com/login/而不是myhost.com/cgi-bin/index.cgi/login/,或者是myhost.com/而不是myhost.com/cgi-bin/index.cgi/ 所有这些较短版本的链接都工作得很好,因为引擎重写规则已经就位。我查过了。只需找到一种方法告诉flask应用程序删除“/cgi-bin/index.cgi/” 我的一些代码:Python 如何使用wsgiref CGIHandler摆脱运行Flask应用程序的额外cgi bin url组件?,python,url,flask,wsgiref,Python,Url,Flask,Wsgiref,我的共享cpanel托管计划不直接支持wsgi应用程序。因此,我必须使用wsgiref CGIHandler解决方案,如下所述: 这一切都可以正常工作并产生预期的结果,但在URL中总是有额外的内容:“/cgi-bin/index.cgi/”,python应用程序似乎会自动添加这些内容(以匹配cgi处理程序调用时检测到的内容) 例如,我希望它是myhost.com/login/而不是myhost.com/cgi-bin/index.cgi/login/,或者是myhost.com/而不是myhos
cat www/.htaccess
# Redirect everything to CGI WSGI handler, but Don't interfere with static files
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cgi-bin/index.cgi/$1 [L]
有什么想法吗
谢谢 我已经找到了一种解决方法。。但这只是一个黑客:-( 如果我在wsgiref CGIHandler脚本中重写SCRIPT_NAME环境变量的值,那么它就可以正常工作 以下是更新的代码:
cat www/cgi-bin/index.cgi
#!/home/myhost/myhost.com/flasky/venv/bin/python
import os
import sys
sys.path.insert(0, '/home/myhost/myhost.com/flasky/venv/lib/python2.7/site-packages')
sys.path.insert(0, '/home/myhost/myhost.com/flasky')
from wsgiref.handlers import CGIHandler
from manage import app
os.environ['SCRIPT_NAME'] = ''
CGIHandler().run(app)
基本上,无论os.environ['SCRIPT\u NAME']值是什么,每次请求页面时都会在flask应用程序url前加上前缀
尽管如此,我仍在寻找更优雅的“pythonic”解决方案。欢迎使用SO,请避免只发布代码答案,并提供一些解释。
cat www/cgi-bin/index.cgi
#!/home/myhost/myhost.com/flasky/venv/bin/python
import os
import sys
sys.path.insert(0, '/home/myhost/myhost.com/flasky/venv/lib/python2.7/site-packages')
sys.path.insert(0, '/home/myhost/myhost.com/flasky')
from wsgiref.handlers import CGIHandler
from manage import app
class ScriptNameStripper(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SCRIPT_NAME'] = ''
return self.app(environ, start_response)
app = ScriptNameStripper(app)
CGIHandler().run(app)
cat www/cgi-bin/index.cgi
#!/home/myhost/myhost.com/flasky/venv/bin/python
import os
import sys
sys.path.insert(0, '/home/myhost/myhost.com/flasky/venv/lib/python2.7/site-packages')
sys.path.insert(0, '/home/myhost/myhost.com/flasky')
from wsgiref.handlers import CGIHandler
from manage import app
os.environ['SCRIPT_NAME'] = ''
CGIHandler().run(app)
cat www/cgi-bin/index.cgi
#!/home/myhost/myhost.com/flasky/venv/bin/python
import os
import sys
sys.path.insert(0, '/home/myhost/myhost.com/flasky/venv/lib/python2.7/site-packages')
sys.path.insert(0, '/home/myhost/myhost.com/flasky')
from wsgiref.handlers import CGIHandler
from manage import app
class ScriptNameStripper(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SCRIPT_NAME'] = ''
return self.app(environ, start_response)
app = ScriptNameStripper(app)
CGIHandler().run(app)