Exemplo n.º 1
0
 def eb(failure):
     self.logger.debug("getJson() eb(%r)", failure)
     if isinstance(failure.type, Exception):
         util.logTwistedFailure(self.logger, failure,
                                "Exception thrown while getting process list")
     self.serverError(request)
     request.write(json.dumps({"error": "500 Internal Server Error"}))
     request.finish()
Exemplo n.º 2
0
 def __getJson(self, request):
     res = self.getJson(request)
     if res is server.NOT_DONE_YET:
         return res
     if type(res) == str:
         return res
     if res == None:
         return ""
     return json.dumps(res) + "\n"
Exemplo n.º 3
0
    def sendRequest(self, url, method, data=None):
        self.logger.debug("sendRequest(%r, %r, %r)", url, method, data)

        if type(data) == dict:
            if self.contentType == "application/json":
                data = json.dumps(data)
            else:
                data = urllib.urlencode(data)
            self.logger.debug("Data after conversion: %r", data)

        headers = {"User-Agent": [self.userAgent], "Accept": [self.accept]}
        if data:
            headers.update({"Content-Type": [self.contentType]})

        if self.extraHeaders:
            headers.update(self.extraHeaders)

        self.logger.debug("Request Headers:")
        if self.logger.isEnabledFor(logging.DEBUG):
            for k, v in headers.iteritems():
                self.logger.debug("\t%r: %r", k, v)

        agent = Agent(reactor)
        deferred = agent.request(method.upper(), url, Headers(headers), FileBodyProducer(StringIO(data)))

        def cb(response):
            self.logger.debug("sendRequest() cb(%r)", response)
            self.logger.info("Request %s %s (Data: %s):", method.upper(), url, data)
            self.logger.info("Response Code: %s", response.code)
            self.logger.debug("Response Phrase: %s", response.phrase)
            self.logger.debug("Response Length: %s", response.length)
            self.logger.debug("Response Headers:")
            if self.logger.isEnabledFor(logging.DEBUG):
                for k, v in response.headers.getAllRawHeaders():
                    self.logger.debug("\t%s: %r", k, v)
            return response

        deferred.addCallback(cb)
        return deferred
Exemplo n.º 4
0
 def cb(processes):
     self.logger.debug("getJson() cb(%r)", processes)
     request.write(json.dumps({"processes": processes}))
     request.finish()
Exemplo n.º 5
0
 def getJson(self, request):
     msg = "Hello JSON: %s" % self.__class__.__name__
     return json.dumps({"message": msg})