Exemplo n.º 1
0
class PiMixedClient():
    def __init__(self, host, port=8000, coap=5683):
        self.host = host
        if coap > 0:
            self.coapport = coap
            self.coapclient = COAPClient()
        else:
            self.coapclient = None
        if port > 0:
            self.httpclient = httplib.HTTPConnection(host, port)
        else:
            self.httpclient = None
        self.forceHttp = False
        self.coapfailure = 0
        self.maxfailure = 2
        self.auth = None

    def setCredentials(self, login, password):
        self.auth = "Basic " + encodeCredentials(login, password)

    def sendRequest(self, method, uri):
        if self.coapclient != None and not self.forceHttp:
            if method == "GET":
                response = self.coapclient.sendRequest(
                    COAPGet("coap://%s:%d%s" %
                            (self.host, self.coapport, uri)))
            elif method == "POST":
                response = self.coapclient.sendRequest(
                    COAPPost("coap://%s:%d%s" %
                             (self.host, self.coapport, uri)))

            if response:
                return str(response.payload)
            elif self.httpclient != None:
                self.coapfailure += 1
                print("No CoAP response, fall-back to HTTP")
                if (self.coapfailure > self.maxfailure):
                    self.forceHttp = True
                    self.coapfailure = 0
                    print("Too many CoAP failure forcing HTTP")

        if self.httpclient != None:
            headers = {}
            if self.auth != None:
                headers["Authorization"] = self.auth

            self.httpclient.request(method, uri, None, headers)
            response = self.httpclient.getresponse()
            if response.status == 200:
                data = response.read()
                return data
            elif response.status == 401:
                raise Exception("Missing credentials")
            else:
                raise Exception("Unhandled HTTP Response %d %s" %
                                (response.status, response.reason))

        raise Exception("No data received")
Exemplo n.º 2
0
class PiMixedClient():
    def __init__(self, host, port=8000, coap=5683):
        self.host = host
        if coap > 0:
            self.coapport = coap
            self.coapclient = COAPClient()
        else:
            self.coapclient = None
        if port > 0:
            self.httpclient = httplib.HTTPConnection(host, port)
        else:
            self.httpclient = None
        self.forceHttp = False
        self.coapfailure = 0
        self.maxfailure = 2
        self.auth= None;
    
    def setCredentials(self, login, password):
        self.auth = "Basic " + encodeCredentials(login, password)
        
    def sendRequest(self, method, uri):
        if self.coapclient != None and not self.forceHttp:
            if method == "GET":
                response = self.coapclient.sendRequest(COAPGet("coap://%s:%d%s" % (self.host, self.coapport, uri)))
            elif method == "POST":
                response = self.coapclient.sendRequest(COAPPost("coap://%s:%d%s" % (self.host, self.coapport, uri)))

            if response:
                return str(response.payload)
            elif self.httpclient != None:
                self.coapfailure += 1
                print("No CoAP response, fall-back to HTTP")
                if (self.coapfailure > self.maxfailure):
                    self.forceHttp = True
                    self.coapfailure = 0
                    print("Too many CoAP failure forcing HTTP")
        
        if self.httpclient != None:
            headers = {}
            if self.auth != None:
                headers["Authorization"] = self.auth
            
            self.httpclient.request(method, uri, None, headers)
            response = self.httpclient.getresponse()
            if response.status == 200:
                data = response.read()
                return data
            elif response.status == 401:
                raise Exception("Missing credentials")
            else:
                raise Exception("Unhandled HTTP Response %d %s" % (response.status, response.reason))

        raise Exception("No data received")
Exemplo n.º 3
0
 def __init__(self, host, port=8000, coap=5683):
     self.host = host
     if coap > 0:
         self.coapport = coap
         self.coapclient = COAPClient()
     else:
         self.coapclient = None
     if port > 0:
         self.httpclient = httplib.HTTPConnection(host, port)
     else:
         self.httpclient = None
     self.forceHttp = False
     self.coapfailure = 0
     self.maxfailure = 2
     self.auth = None
Exemplo n.º 4
0
 def __init__(self, host, port=8000, coap=5683):
     self.host = host
     if coap > 0:
         self.coapport = coap
         self.coapclient = COAPClient()
     else:
         self.coapclient = None
     if port > 0:
         self.httpclient = httplib.HTTPConnection(host, port)
     else:
         self.httpclient = None
     self.forceHttp = False
     self.coapfailure = 0
     self.maxfailure = 2
     self.auth= None;