Forums

API Usage

I'm on a hacker's account and have created and deployed a few flask apis. When I reload, the first api works and when testing another it results in a 505 error until I reload again.

Please advise.

505 is a very strange error to get. It means that the the server does not support the version of HTTP that the client is using. What client are you using to access your account?

I'm don't really understand your question, so do correct me if I'm wrong.

We are using an external database (ElephantSQL) the free version which our APIs will be interacting with.

2023-03-09 05:28:38 Thu Mar 9 05:28:38 2023 - received message 0 from emperor 2023-03-09 05:28:38 SIGINT/SIGTERM received...killing workers... 2023-03-09 05:28:39 worker 1 buried after 1 seconds 2023-03-09 05:28:39 worker 2 buried after 1 seconds 2023-03-09 05:28:39 goodbye to uWSGI. 2023-03-09 05:28:39 VACUUM: unix socket /var/sockets/is484testing.pythonanywhere.com/socket removed. 2023-03-09 05:28:45 *** Starting uWSGI 2.0.20 (64bit) on [Thu Mar 9 05:28:42 2023] *** 2023-03-09 05:28:45 compiled with version: 9.4.0 on 22 July 2022 18:35:26 2023-03-09 05:28:45 os: Linux-5.15.0-1021-aws #25~20.04.1-Ubuntu SMP Thu Sep 22 13:59:08 UTC 2022 2023-03-09 05:28:45 nodename: green-liveweb14 2023-03-09 05:28:45 machine: x86_64 2023-03-09 05:28:45 clock source: unix 2023-03-09 05:28:45 pcre jit disabled 2023-03-09 05:28:45 detected number of CPU cores: 4 2023-03-09 05:28:45 current working directory: /home/IS484testing 2023-03-09 05:28:45 detected binary path: /usr/local/bin/uwsgi 2023-03-09 05:28:45 *** dumping internal routing table *** 2023-03-09 05:28:45 [rule: 0] subject: path_info regexp: .svgz$ action: addheader:Content-Encoding:gzip 2023-03-09 05:28:45 *** end of the internal routing table *** 2023-03-09 05:28:45 chdir() to /home/IS484testing/ 2023-03-09 05:28:45 your processes number limit is 256 2023-03-09 05:28:45 your memory page size is 4096 bytes 2023-03-09 05:28:45 detected max file descriptor number: 123456 2023-03-09 05:28:45 building mime-types dictionary from file /etc/mime.types... 2023-03-09 05:28:45 567 entry found 2023-03-09 05:28:45 lock engine: pthread robust mutexes 2023-03-09 05:28:45 thunder lock: disabled (you can enable it with --thunder-lock) 2023-03-09 05:28:45 uwsgi socket 0 bound to UNIX address /var/sockets/is484testing.pythonanywhere.com/socket fd 3 2023-03-09 05:28:45 Python version: 3.10.5 (main, Jul 22 2022, 17:09:35) [GCC 9.4.0] 2023-03-09 05:28:45 *** Python threads support is disabled. You can enable it with --enable-threads *** 2023-03-09 05:28:45 Python main interpreter initialized at 0x558993a63e70 2023-03-09 05:28:45 your server socket listen backlog is limited to 100 connections 2023-03-09 05:28:45 your mercy for graceful operations on workers is 60 seconds 2023-03-09 05:28:45 setting request body buffering size to 65536 bytes 2023-03-09 05:28:45 mapped 501384 bytes (489 KB) for 2 cores 2023-03-09 05:28:45 *** Operational MODE: preforking *** 2023-03-09 05:28:45 initialized 54 metrics 2023-03-09 05:28:45 WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x558993a63e70 pid: 1 (default app) 2023-03-09 05:28:45 *** uWSGI is running in multiple interpreter mode *** 2023-03-09 05:28:45 gracefully (RE)spawned uWSGI master process (pid: 1) 2023-03-09 05:28:45 spawned uWSGI worker 1 (pid: 2, cores: 1) 2023-03-09 05:28:45 spawned 2 offload threads for uWSGI worker 1 2023-03-09 05:28:45 spawned uWSGI worker 2 (pid: 5, cores: 1) 2023-03-09 05:28:45 metrics collector thread started 2023-03-09 05:28:45 announcing my loyalty to the Emperor... 2023-03-09 05:28:45 spawned 2 offload threads for uWSGI worker 2 2023-03-09 05:29:14 announcing my loyalty to the Emperor...

2023-03-09 06:02:55 *** Starting uWSGI 2.0.20 (64bit) on [Thu Mar 9 06:02:54 2023] *** 2023-03-09 06:02:55 compiled with version: 9.4.0 on 22 July 2022 18:35:26 2023-03-09 06:02:55 os: Linux-5.15.0-1021-aws #25~20.04.1-Ubuntu SMP Thu Sep 22 13:59:08 UTC 2022 2023-03-09 06:02:55 nodename: green-liveweb14 2023-03-09 06:02:55 machine: x86_64 2023-03-09 06:02:55 clock source: unix 2023-03-09 06:02:55 pcre jit disabled 2023-03-09 06:02:55 detected number of CPU cores: 4 2023-03-09 06:02:55 current working directory: /home/IS484testing 2023-03-09 06:02:55 detected binary path: /usr/local/bin/uwsgi 2023-03-09 06:02:55 *** dumping internal routing table *** 2023-03-09 06:02:55 [rule: 0] subject: path_info regexp: .svgz$ action: addheader:Content-Encoding:gzip 2023-03-09 06:02:55 *** end of the internal routing table *** 2023-03-09 06:02:55 chdir() to /home/IS484testing/ 2023-03-09 06:02:55 your processes number limit is 256 2023-03-09 06:02:55 your memory page size is 4096 bytes 2023-03-09 06:02:55 detected max file descriptor number: 123456 2023-03-09 06:02:55 building mime-types dictionary from file /etc/mime.types... 2023-03-09 06:02:55 567 entry found 2023-03-09 06:02:55 lock engine: pthread robust mutexes 2023-03-09 06:02:55 thunder lock: disabled (you can enable it with --thunder-lock) 2023-03-09 06:02:55 uwsgi socket 0 bound to UNIX address /var/sockets/is484testing.pythonanywhere.com/socket fd 3 2023-03-09 06:02:55 Python version: 3.10.5 (main, Jul 22 2022, 17:09:35) [GCC 9.4.0] 2023-03-09 06:02:55 *** Python threads support is disabled. You can enable it with --enable-threads *** 2023-03-09 06:02:55 Python main interpreter initialized at 0x56118b6ace70 2023-03-09 06:02:55 your server socket listen backlog is limited to 100 connections 2023-03-09 06:02:55 your mercy for graceful operations on workers is 60 seconds 2023-03-09 06:02:55 setting request body buffering size to 65536 bytes 2023-03-09 06:02:55 mapped 501384 bytes (489 KB) for 2 cores 2023-03-09 06:02:55 *** Operational MODE: preforking *** 2023-03-09 06:02:55 initialized 54 metrics 2023-03-09 06:02:55 postgres://odaaktsj:cKcyF_09f7aYsEZe6IOUWeGPpYFAdZZZ@tiny.db.elephantsql.com/odaaktsj 2023-03-09 06:02:55 <connection object at 0x7f482896f100; dsn: 'user=odaaktsj password=xxx dbname=odaaktsj host=tiny.db.elephantsql.com', closed: 0> 2023-03-09 06:02:55 WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x56118b6ace70 pid: 1 (default app) 2023-03-09 06:02:55 *** uWSGI is running in multiple interpreter mode *** 2023-03-09 06:02:55 gracefully (RE)spawned uWSGI master process (pid: 1) 2023-03-09 06:02:55 spawned uWSGI worker 1 (pid: 2, cores: 1) 2023-03-09 06:02:55 spawned 2 offload threads for uWSGI worker 1 2023-03-09 06:02:55 spawned uWSGI worker 2 (pid: 5, cores: 1) 2023-03-09 06:02:55 metrics collector thread started 2023-03-09 06:02:55 spawned 2 offload threads for uWSGI worker 2 2023-03-09 06:02:56 announcing my loyalty to the Emperor... 2023-03-09 06:03:13 announcing my loyalty to the Emperor...

Above is the server error logs after I updated my wsgi and flask_app.py file. I am now able to run my api 3 times before the Internal Server error is shown.

Appreciate any help given.

This is my edited wsgi code:

# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project

import sys
# import os
# from dotenv import load_dotenv

# api_folder = os.path.expanduser('~/mysite')
# load_dotenv(os.path.join(api_folder, '.env'))

# add your project directory to the sys.path
project_home = '/home/IS484testing/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from flask_app import app as application  # noqa

This is my code for flask_app.py:

import os
import psycopg2
from dotenv import load_dotenv
from flask import Flask, request

api_folder = os.path.expanduser('~/mysite')
load_dotenv(os.path.join(api_folder, '.env'))

######## DB CONNECTION ########
url = os.environ.get("POSTGRESQL_CONNECTION_STRING")

print(url)

connection = psycopg2.connect(url)

print(connection)

app = Flask(__name__)

# ######## APIs ########

@app.get("/get_companies")
def get_companies():
    with connection:
        with connection.cursor() as cursor:
            companies = (
                """SELECT * FROM companies;"""
            )
            cursor.execute(companies)
            results = cursor.fetchall()
            response = {
                "data": results,
                "code": 200
            }
            if (len(results)==0):
                response["status"] = "No companies available"

    return response

Where .env file contains the link to an external postgress database (ElephantSQL)

You're trying to keep the connection open forever and it's likely that there is a timeout on how long you can keep a connection open. If you create the connection in get_companies and then close it at the end of the function, that may help.