Example #1
0
                        return # Bör ersättas med felkod. Kommer vi hit är något allvarligt fel
                else:
                    self._writetextheaders()
                    self.send_result(message)
                    return
        elif command == "keywords":
            data = data.replace('*', ' ')
            keys = data.split(",")
            frienemy_result = tallstore.get_frienemies_by_keywords(keys)
        else:
            self._writetextheaders()
            self.send_result("Error: Bad argument, please verify your input.") 
            return
        if frienemy_result == "Error: Solr connection." or frienemy_result == "Error: Unknown error.":
            self._writetextheaders()
            self.send_result(GENERICERROR)
            return
        self._writexmlheaders()
        self.send_result(create_xml(frienemy_result))



if __name__ == "__main__":        
    # This is for starting the server.
    print "Connecting to Solr"
    tallstore.connect_to_solr()
    serveraddr = ('', 8001)
    srvr = ThreadingServer(serveraddr, RequestHandler)
    print "Server started"
    srvr.serve_forever()
    def do_GET(self):
        # KLUDGE: by always reconnecting to Solr we can handle the connection to Solr going down
        # TODO: implement a nicer solution to this by reconnecting only when handling the exception raised when Solr is unreachable
        try:
            tallstore.connect_to_solr()
        except Exception:
            sys.stderr.write("Unhandled exception when connecting to Solr:\n")
            traceback.print_exc()
            self._writetextheaders()
            self.send_result(GENERICERROR)
            return
        
        print self.requestline
        print self.path
        if self.path == "/favicon.ico":
            return
        if self.path == '/':
            return
        command, data = get_arguments(self.path)
#        data = data.lower()
        print "Command: " + command
        print "Data: " + data

        ################
        ######TEST######
        ################
        # ignore anything but the SSDummies
        if command == "testusername":
            frienemy_result = tallstore.get_frienemies_by_id(data, test_mode=True)
            self._writexmlheaders()
            self.send_result(create_xml(frienemy_result))
            return
        ################
        ######TEST######
        ################

        if command == "username":
            data = data.replace('*', '')
            print "new data: " + data
            frienemy_result = tallstore.get_frienemies_by_id(data) # Ska ersättas med anrop till storage handler
            if frienemy_result == False:
                succeeded, message = send_to_request(data)
#                succeeded = False
#                message = "Request is not online. Cannot retrieve new users from Twitter."
#                print message
                print "Succeeded: " + str(succeeded)
                print message
                if succeeded == True:
                    # Hämta från storage
                    frienemy_result = tallstore.get_frienemies_by_id(data)
                    if frienemy_result == False:
                        return # Bör ersättas med felkod. Kommer vi hit är något allvarligt fel
                else:
                    self._writetextheaders()
                    self.send_result(message)
                    return
        elif command == "keywords":
            data = data.replace('*', ' ')
            keys = data.split(",")
            frienemy_result = tallstore.get_frienemies_by_keywords(keys)
        else:
            self._writetextheaders()
            self.send_result("Error: Bad argument, please verify your input.") 
            return
        if frienemy_result == "Error: Solr connection." or frienemy_result == "Error: Unknown error.":
            self._writetextheaders()
            self.send_result(GENERICERROR)
            return
        self._writexmlheaders()
        self.send_result(create_xml(frienemy_result))