Я столкнулся с ошибкой сервера 500 при попытке подключиться к PostgreSQL из файла wsgi с помощью psycopg2.
import psycopg2
def application(environ, start_response):
try:
conn = psycopg2.connect("database = testdb, user = postgres, password = secret")
execept:
print "I am unable to connect to the database"
status = '200 OK'
output = 'Hello Udacity, Robert!'
response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Да, журнал ошибок сервера: 10.0.2.2 - - [21 / Oct / 2018: 03: 05: 37 +0000] "GET / HTTP / 1.1" 500 801 "-" "Mozilla / 5.0 (Windows NT 10.0; Win64 ; x64) AppleWebKit / 537.36 (KHTML, например, Gecko) Chrome / 69.0.3497.100 Safari / 537.36 "10.0.2.2 - - [21 / октября / 2018: 03: 05: 40 +0000]" GET /favicon.ico HTTP / 1.1 "500 801" локальный: 8080 "" Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, например Gecko) Chrome / 69.0.3497.100 Safari / 537.36 "
Собственно, я решил проблему. Похоже, что как-то мой psycopg2 не работает, поэтому мне пришлось его переустановить





После создания базы данных (в моем случае - udacity), таблицы (в моем случае - hello) и населения (в моем случае - Hello world):
# psql environment
CREATE DATABASE udacity;
CREATE TABLE hello(
word text);
INSERT INTO hello VALUES ('Hello world');
Вам необходимо установить зависимости Python:
# shell environment
sudo apt install python-psycopg2 libpq-dev
sudo apt install python-pip
pip install psycopg2
Затем у меня работает следующий скрипт myapp.wsgi:
import psycopg2
def application(environ, start_response):
status = '200 OK'
output = 'Original message'
response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
start_response(status, response_headers)
# DB connection
try:
connection = psycopg2.connect("dbname='udacity' user='ubuntu' host='localhost' password='udacity'")
cursor = connection.cursor()
cursor.execute("""SELECT * from hello""")
rows = cursor.fetchall()
for row in rows:
output = row[0]
except:
output = 'E: Python script'
return [output]
Думаю, в вашем вопросе отсутствует сообщение об ошибке.