I am having trouble getting Passenger to work with Flask. I am running the Flask site on DreamHost inside a virutalenv running Python 3.5.2. The application should receive input from a query string in the URL, parse a server-side JSON file, search the JSON file for said query string, reformat the JSON, and send it back to the client. Instead, it gives me the 500 error code. Here is my code. Please pardon all the logging function calls. I've done what I can by searching around.
import logging
logging.basicConfig(filename='wsgi_flask.log', level=logging.DEBUG)
try:
import sys, os
logging.info('line 7')
VENV = os.path.join(os.environ['HOME'], '.pyenv', 'versions', '3.5.2', 'envs', 'flask')
INTERP = os.path.join(VENV, 'bin', 'python')
logging.info('line 12')
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
logging.info('line 17')
sys.path.append(VENV)
sys.path.append(os.path.join(VENV, 'lib', 'python3.5', 'site-packages'))
sys.path.append(os.path.join(os.environ['HOME'], 'api.example.com'))
logging.info('line 23')
# INTERP = os.path.join(os.environ['HOME'], '.pyenv', 'versions', 'flask', 'bin', 'python')
# if sys.executable != INTERP:
# os.execl(INTERP, INTERP, *sys.argv)
# sys.path.append(os.getcwd())
from paste.exceptions.errormiddleware import ErrorMiddleware
logging.info('line 31')
# import argparse
import json
logging.info('line 36')
from flask import (Flask, abort, g, jsonify, make_response, redirect,
render_template, render_template_string, request, url_for, Response)
logging.info('line 41')
# parser = argparse.ArgumentParser()
# parser.add_argument('-d', '--debug', action='store_true')
# apargs = parser.parse_args()
# def debugLog(*args):
# if apargs.debug:
# for i in args:
# print(i)
application = Flask(__name__)
logging.info('line 54')
@application.route('/')
def index():
logging.info('line 58')
# if apargs.debug:
# data = json.load(open('/path/to/test.json', 'rb'))
# else:
logging.info('line 62')
try:
logging.info('line 64')
data = json.load(open('/path/to/test.json', 'rb'))
logging.info('line 66')
except:
logging.info('line 68')
logging.warning('Is it the JSON?')
raise('Error whilst loading JSON')
logging.info('line 71')
getForm = request.args.get('search') or ''
logging.info('line 73')
# debugLog(data, getForm)
jsonData = ""
logging.info('line 78')
for d in data:
logging.info('line 80 (for loop)')
# debugLog(d)
if getForm in d['text']:
logging.info('line 83 (for loop)')
jsonData = jsonData + str(d) + ','
logging.info('line 85 (for loop)')
logging.info('line 86 (for loop)')
logging.info('line 87 (out of loop)')
# debugLog(jsonData)
jsonData = str(jsonData)
logging.info('line 91')
jsonData = jsonData[:-1]
logging.info('line 93')
jsonData = jsonData.replace("'", '"')
logging.info('line 95')
jsonData = '[' + jsonData + ']'
logging.info('line 97')
# debugLog('\n', jsonData)
return Response(jsonify(json.loads(jsonData)), mimetype='application/json')
logging.info('line 101 (this should not show)')
@application.errorhandler(500)
def error(e):
logging.warning(e + '\n\n\n What\'s happening here')
return str(e)
logging.info('line 107')
application = ErrorMiddleware(application, debug=True, error_log='tmp/wsgi_error.log')
logging.info('line 109')
# if apargs.debug:
# application.run(host='0.0.0.0', debug=True, port=80)
except:
logging.warning('line 114 (beginning of outer most try-except)')
import sys, os
logging.warning('line 116')
from traceback import format_list, extract_tb
logging.warning('line 119')
(extype, value, trace) = sys.exc_info()
logging.warning('line 121')
sys.stderr.buffer.write("%s:%s\n%s" % (extype, value,''.join(format_list(extract_tb(trace)))))
logging.warning('line 124 (EOF)')
This is my directory tree:
.
|-- .python-version
|-- public
| |-- .htaccess
| |-- favicon.gif
| |-- favicon.ico
| |-- passenger_wsgi.py
| `-- quickstart.html
`-- tmp
`-- restart.txt
And this is my .htaccess:
PassengerPython /home/dh_user/.pyenv/versions/3.5.2/envs/flask/bin/python
<VirtualHost *:80>
# This Flask web app will use Python 3.0
ServerName api.example.com
DocumentRoot /home/dh_user/api.example.com/public
</VirtualHost>
I have used all the guides below for help and reference:
https://help.dreamhost.com/hc/en-us/articles/215769548-Passenger-and-Python-WSGI https://www.phusionpassenger.com/library/walkthroughs/start/python.html https://www.byteconsole.com/get-flask-up-and-running-on-a-dreamhost-server-with-passenger/
Edit: Dreamhost doesn't allow me to look at the Passenger logs on a shared host. The best I can give you is my error.log, which, on every request, gives me
[Wed Apr 26 13:52:51 2017] [error] [client xxx.xxx.xxx.xxx] Premature end of script headers:
[Wed Apr 26 13:52:51 2017] [error] [client xxx.xxx.xxx.xxx] Premature end of script headers: internal_error.html
Edit 2: I'm using the Paste Python module to collect errors from Flask and it's giving me this:
File '/home/dh_user/.pyenv/versions/3.5.2/envs/flask/lib/python3.5/site-packages/paste/exceptions/errormiddleware.py', line 224 in next
return self.app_iterator.next()
AttributeError: 'ClosingIterator' object has no attribute 'next'