We still have some logical errors in our WebServer. Right now, when we refference an operator that is neither a DAT nor a TOP, we return a response without any data, but still suggest the browser that everything is OK because we send a statusCode 200. So lets rethink our way of handling it. Right now, we check if the operator exists. Only if it does not we set our status code to be a 404.
So, lets set our status code to 404 from the beginning and only set it to 200 if we actually were able to retrieve any data from an operator.
The response dictionary is empty first and only gets filled with information by us. So we can just check if ‘data’ is a part of our response dictionary using the in statement.
if 'data' in response: response['statusCode'] = 200 # OK response['statusReason'] = 'OK'
Out final webserver now looks like this:
def onHTTPRequest(webServerDAT, request, response): response['statusCode'] = 404 #Missing File response['statusReason'] = 'Operator does not exist' target_operator = op( request['uri'] ) if target_operator is None: return response if target_operator.isDAT: response['data'] = target_operator.text if target_operator.isTOP: response['data'] = target_operator.saveByteArray('.png') if 'data' in response: response['statusCode'] = 200 # OK response['statusReason'] = 'OK' return response
To get a better feeling for HTML and how to write a website i highly recommend visiting https://www.w3schools.com