0

I've been troubleshooting my RPi and I keep getting an internal 500 error in my errors.log for a script written in python. I actually get 2 errors in the logs, wondering how I would go about fixing them

@berryGate:~/usb-scanner $ tail errors.log
2017-08-01 08:02:13 [URL Error] While sending data to remote Server: <urlopen error [Errno -5] No address associated with hostname>
2017-08-01 09:20:56 [HTTP Error]    While sending data to remote Server: HTTP Error 500: Internal Server Error
2017-08-01 12:12:22 [HTTP Error]    While sending data to remote Server: HTTP Error 500: Internal Server Error
2017-08-01 13:01:42 [HTTP Error]    While sending data to remote Server: HTTP Error 500: Internal Server Error

So it's 2 errors, the HTTP and URL error -5.

Ok so the Script is written in python

Stack DigitalOcean VPS: Ubuntu 16.04 (32 bit due to low VPS resources), Python3, Django, PostgreSQL, Gunicorn, NGINX

Stack info and build

The script authenticates to a server and allows pass to a security gate. It is working, both entrance and exit, but sometimes it will stick and i fear its due to this error. I did not write this code, and I don't code, so im just trying to do the best I can.

Here is the beginning and end of the script. Please let me know if there's a better way for me to post this.

#!/usr/bin/python3

    -Listens continuously for two usb scanner located in /dev/input/by-path/
-A correct scanner code must meet the following criteria:
        * Starts with one of <CODE_PREFIXES>
        * Ends with <TERMINATION_CHAR>
        * #<CODE_PREFIX> + #code + #<TERMINATION_CHAR> = 1 + <CODE_LENGTH> + 1 (                                                                                        # = length of)
-All cought errors will get logged in a specified file
-A local database will be fetched from the remote server and kept updated every                                                                                         x minutes
-Offers buttons to restart the RPI, power off the RPI, and updating local databa                                                                                        se
"""


import struct

import sys

import os, os.path

import time

import datetime

import json

import urllib.parse, urllib.request, urllib.error
from threading import Thread, Event

import pifacedigitalio as piface


# GLOBALS
RELAY_ONE = 0
RELAY_TWO = 1
CUR_PATH = os.path.abspath(os.path.dirname(__file__))
error_occurred = False
debug_loop_funcs = []



 ###### THIS HAS BEEN SHORTENED TO THE END######

        try:
            with open(DATABASE_PATH) as f:
                self.database = json.load(f)
        except FileNotFoundError as e:
            with open(DATABASE_PATH, 'w') as f:
                pass
        except json.decoder.JSONDecodeError as e:
            logError("JSON", "Local database is not valid JSON")

    def writeDatabase(self):
        with open(DATABASE_PATH, 'w') as f:
            json.dump(self.database, f)

    def updateDatabase(self):
        dprint(1, "Updating database...")
        data = {"key": self.server.api_key}
        success = False
        try:
            response = json.loads(self.server.get(SERVER_DATABASE_PATH, data))
            success = True
            if self.checkResponse(response) is not None:
                self.database = response["data"]
        except Server.HTTPError as e:
            logError("HTTP Error", "On remote database update query: %s" %e)
        except Server.URLError as e:
            logError("URL Error", "On remote database update query: %s" %e)
        except json.decoder.JSONDecodeError as e:
            logError("JSON", "Remote database is not valid JSON")

        return success

    def onReceiveCode(self, name, full_code, stripped_code, filename):
        granted = False
        if stripped_code in self.database:
            granted = True
            Thread(target=toggleRelay, args=(CODE_PREFIXES[name][1],)).start()

        data = {
            "key" : self.server.api_key,
            "gate" : name,
            "code" : stripped_code,
            "timestamp" : datetime.datetime.fromtimestamp(time.time()).strftime(                                                                                        "%Y-%m-%d %H:%M:%S"),
            "allow" : granted
        }
        dprint(1, "[%s]: %s | %s | Access granted: %s" %(name, full_code, filena                                                                                        me, granted))

        try:
            response = json.loads(self.server.send(SERVER_ADD_PATH, data))
            self.checkResponse(response)
        except Server.HTTPError as e:
            logError("HTTP Error", "While sending data to remote Server: %s" %e)
        except Server.URLError as e:
            logError("URL Error", "While sending data to remote Server: %s" %e)
        except json.decoder.JSONDecodeError as e:
            logError("JSON", "Remote server response is not valid JSON")

    def onConnectScanner(self, name, filename):
        dprint(1, "[%s] connected! (%s)" %(name, filename))

    def onDisconnectScanner(self, name, filename):
        dprint(1, "[%s] disconnected! (%s)" %(name, filename))

    def checkResponse(self, response):
        if response["status"] == "success":
            return response
        logError("Server", response["msg"])
        return None


if __name__ == "__main__":
    lf = [] if not DEBUG else debug_loop_funcs
    with Main(loop_funcs=lf) as m:
        pass
  • Why is all your code stored in a string? – Jacobm001 Aug 3 '17 at 22:25
  • What do you mean stored in a string? – cyberboli Aug 14 '17 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.