def __init__(self):
     if DataBase._instace != None:
         raise Exception("This is a singleton. Call GetInstance()")
     else:
         Logging.WriteToLog("Making DataBaseConnection")
         self.connection = self._connectToDataBase()
         DataBase._instace = self
 def FindHttpServerPortDynamically(self):
     while (True):
         try:
             self.serversocket = socket(AF_INET, SOCK_STREAM)
             self.serversocket.bind(
                 (self.ServerIp,
                  self.ServerPortNum))  # this is the line that fails
             self.WebsocketPortNum = self.ServerPortNum + 1
             Logging.WriteToLog('Access http://' + self.ServerIp + ':' +
                                str(self.ServerPortNum))
             return
         except Exception as exc:
             Logging.WriteToLog("Error Finding port " +
                                str(self.ServerPortNum) + ":\n")
             Logging.WriteToLog(exc)
             self.ServerPortNum += 1
 def Print(self):
     Logging.WriteToLog('----------http message----------------')
     Logging.WriteToLog('command: ' + self.command)
     Logging.WriteToLog('path: ' + self.path)
     Logging.WriteToLog('pathRoute: ')
     Logging.WriteToLog(self.pathRoutes)
     Logging.WriteToLog('urlArgs: ')
     Logging.WriteToLog(self.urlArgs)
     Logging.WriteToLog('version: ' + self.version)
     Logging.WriteToLog('--headers--')
     for key, value in self.headers.items():
         Logging.WriteToLog(key + ": " + value)
         print()
     print('--cookies--')
     for key, value in self.cookies.items():
         print(key + " = " + value)
     print('--body--')
     print(self.body)
 def __init__(self, socket):
     #TODO be able to receive large messages
     rawinput = socket.recv(5000).decode()
     Logging.WriteToLog("bytes received: " + str(len(rawinput)) + "\n")
     lines = rawinput.split(self.new_line)
     self.ParseFirstLine(lines[0])
     #parse headers
     headersLastLine = lines.index("")
     self.ParseHeaders(lines[1:headersLastLine])
     self.ParseBody(socket, lines[headersLastLine + 1::])
 def AddNewUser(self,userName, password):
     cursor = self.connection.cursor()
     add_user = ("INSERT INTO User "
            "(userName, password) "
            "VALUES (%s, %s)")
     try:            
         cursor.execute(add_user, (userName, self.get_hashed_password(password.encode())))
     except Exception as e:
         Logging.WriteToLog("Error adding User: "******"\n" + str(e))
         cursor.close()
         return False
     self.connection.commit()
     cursor.close()
     return True