예제 #1
0
    def get_data(self, url, firmware=False):
        print("GET-DATA")
        
        # Connect to server
        try:
            proto, dummy, host, path = url.split("/", 3)
        except ValueError:
            proto, dummy, host = url.split("/", 2)
            path = ""
        if proto == "http:":
            port = 80
        elif proto == "https:":
            import ussl
            port = 443
        else:
            raise ValueError("Unsupported protocol: " + proto)

        ai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
        ai = ai[0]
        print("Requesting: {}".format(path))
        s = socket.socket(ai[0], ai[1], ai[2])
        s.connect(ai[-1])
        s = ussl.wrap_socket(s, server_hostname=host)

        # Request File
        s.sendall(self._http_post(path, host, cid=self.cid))
        try:
            content = bytearray()
            fp = None
            if firmware:
                pycom.ota_start()

            # Get data from server
            result = s.recv(100)
            start_writing = False
            while (len(result) > 0):
                # Ignore the HTTP headers
                if not start_writing:
                    if "\r\n\r\n" in result:
                        print("result: ", result)
                        start_writing = True
                        result = result.decode().split("\r\n\r\n")[1].encode()
                        filepath = result.decode().split("version")[0].encode()
                        self.destinationPath = '/flash/config.py'
                        print("File Path --> ", self.destinationPath)
                        dest_path = "{}.new".format(self.destinationPath)
                        fp = open(dest_path, 'wb')
        

                if start_writing:
                    if firmware:
                        pycom.ota_write(result)
                    elif fp is None:
                        content.extend(result)
                    else:
                        fp.write(result)

                result = s.recv(100)
                
            s.close()

            if fp is not None:
                fp.close()
            if firmware:
                pycom.ota_finish()

        except Exception as e:
             print("Error", e)
예제 #2
0
    def get_data(self, req, dest_path=None, hash=False, firmware=False):
        # Connect to server
        print("Requesting: {}".format(req))
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                          socket.IPPROTO_TCP)
        s.connect((self.ip, self.port))

        # Request File
        s.sendall(self._http_get(req, "{}:{}".format(self.ip, self.port)))

        try:
            content = bytearray()
            fp = None
            if dest_path is not None:
                if firmware:
                    raise Exception("Cannot write firmware to a file")
                fp = open(dest_path, 'wb')

            if firmware:
                pycom.ota_start()

            h = uhashlib.sha1()

            # Get data from server
            result = s.recv(100)

            start_writing = False
            while (len(result) > 0):
                # Ignore the HTTP headers
                if not start_writing:
                    if "\r\n\r\n" in result:
                        start_writing = True
                        result = result.decode().split("\r\n\r\n")[1].encode()

                if start_writing:
                    if firmware:
                        pycom.ota_write(result)
                    elif fp is None:
                        content.extend(result)
                    else:
                        fp.write(result)

                    if hash:
                        h.update(result)

                result = s.recv(100)

            s.close()

            if fp is not None:
                fp.close()
            if firmware:
                pycom.ota_finish()

        except Exception as e:
            # Since only one hash operation is allowed at Once
            # ensure we close it if there is an error
            if h is not None:
                h.digest()
            raise e

        hash_val = ubinascii.hexlify(h.digest()).decode()

        if dest_path is None:
            if hash:
                return (bytes(content), hash_val)
            else:
                return bytes(content)
        elif hash:
            return hash_val
예제 #3
0
    def get_data(self, req, dest_path=None, hash=False, firmware=False):
        h = None

        useSSL = int(self.port) == 443

        # Connect to server
        print("Requesting: {} to {}:{} with SSL? {}".format(
            req, self.ip, self.port, useSSL))
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                          socket.IPPROTO_TCP)
        s.connect(socket.getaddrinfo(self.ip, self.port)[0][-1])
        if (int(self.port) == 443):
            print("Wrapping socket")
            s = ssl.wrap_socket(s)

        print("Sending request")
        # Request File
        s.sendall(self._http_get(req, "{}:{}".format(self.ip, self.port)))

        try:
            content = bytearray()
            fp = None
            if dest_path is not None:
                print('dest_path {}'.format(dest_path))
                if firmware:
                    raise Exception("Cannot write firmware to a file")
                fp = open(dest_path, 'wb')

            if firmware:
                print('start')
                pycom.ota_start()

            h = uhashlib.sha1()

            # Get data from server
            result = s.recv(50)
            print_debug(4, "Result: {}".format(result))

            start_writing = False
            while (len(result) > 0):
                # Ignore the HTTP headers
                if not start_writing:
                    if "\r\n\r\n" in result:
                        start_writing = True
                        result = result.split(b'\r\n\r\n')[1]

                if start_writing:
                    if firmware:
                        pycom.ota_write(result)
                    elif fp is None:
                        content.extend(result)
                    else:
                        fp.write(result)

                    if hash:
                        h.update(result)

                result = s.recv(50)

            s.close()

            if fp is not None:
                fp.close()
            if firmware:
                print_debug(6, 'ota_finish')
                pycom.ota_finish()

        except Exception as e:
            gc.mem_free()
            # Since only one hash operation is allowed at Once
            # ensure we close it if there is an error
            if h is not None:
                h.digest()
            raise e

        hash_val = ubinascii.hexlify(h.digest()).decode()

        if dest_path is None:
            if hash:
                return (bytes(content), hash_val)
            else:
                return bytes(content)
        elif hash:
            return hash_val