コード例 #1
0
ファイル: __init__.py プロジェクト: kd8bxp/RasPiRobot-Arduino
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")
コード例 #2
0
ファイル: clients.py プロジェクト: hesperus22/WebIOPi
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")
コード例 #3
0
ファイル: __init__.py プロジェクト: xie-xuan/rpi3-webiopi
 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
コード例 #4
0
ファイル: clients.py プロジェクト: hesperus22/WebIOPi
 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;