In psycopg2, there is an attribute in both the cursor and connection objects named "closed".
For example, to check if your cursor still is open:
connection = psycopg2.connect (...) cursor = connection.cursor() if cursor.closed: print('the connection is closed') else: ...