python - Deploy flask application on 1&1 shared hosting (with CGI) -


i've written web application sports club flask web framework. did on local machine build-in test server.

know told me deploy on 1&1 shared hosting web space. have python support seems allow cgi run python scripts.

i tried tutorial: flask via cgi

i ignored rewrite stuff until now. requests cgi script resulted in 404 error. modified 404 handler in application return request.path. when request /foo/runserver.cgi/ returns / output. have no idea why doesn't serve index view. doesn't work view, 404.

kind regards, sebastian

i'm writing in provide answer after year because given answer incomplete , because suggestion leave off /$1 wrong. other stackoverflow threads can reached internet search using string "deploy flask on cgi" have ended without satisfactory solutions.

to begin, .htaccess file in referenced "flask via cgi" doc, except comment in second line rewritecond has removed because in .htaccess comment must occupy entire line.

i put .htaccess file in public_html document root folder , cgi script /home/myusername/public_html/scgi-bin/moc/cgiappserver-prod.cgi.

it's python of course , shebang @ top had better right. @ isp use cpanel has wrapper cgi call "scgi". it's not the real thing, unfortunately. treat ordinary cgi purposes of running flask.

i should add have shared-hosting account.

here's cgiappserver-prod.cgi file:

#!/home/myusername/local/bin/python import cgitb; cgitb.enable()  # line enables cgi error reporting wsgiref.handlers import cgihandler import traceback settings import lggr  app = none try:     import moc     app = moc.app except exception, e:     lggr.info( traceback.format_exc([10]) )     lggr.info( 'problem in cgiappserver-prod moc import: %s' % e )  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)  try:     cgihandler().run(app) except exception, e:     lggr.info( traceback.format_exc([10]) )     lggr.info( 'problem in cgiappserver-prod cgihandler().run(): %s' % e ) 

so app spread on few files, setting.py , moc.py in particular showing in code above.

my hours of foundering around partly due of unhelpful posts on subject read, due not getting business of getting error messages out enough. (i have access error log provided isp seldom helpful.)

to start, have confirmed cgitb.enable() function works. have deliberately misspelled wsgiref , seen beautiful error page , have commented out cgitb (cgi traceback) line see error message turn useless 500 status code.

note set in settings.py logger, rotating file logger lggr. discovered had extra--- not shown here--- tell python interpreter sqlite3 library is.

also, can use print statements, referenced flask docs on cgi say:

  • with cgi, have make sure code not contain print statements, or sys.stdout overridden doesn’t write http response.

that's true, it's helpful while debugging see print write http response.

finally, when got working location box of browser sadly had stuff www.mysite.com/scgi-bin/moc/cgiappserver-prod.cgi/contact in it, whereas needed www.mysite.com/contact.

the cure scriptnamestripper class in cgiappserver-prod.cgi. got other flask docs.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -