(self, *args, **kwargs)
return (hdr, buf)
http get implementation using the curl cli executable
http get implementation using the curl cli executable
http get implementation using the curl cli executable
def get(self, *args, **kwargs):
        """http get implementation using the curl cli executable"""
        hdr = CurlHeader()
        buf = strio()
        self._set_defaults()
        cmd = self._mkcurlcmd(*args, **kwargs)
        status = Uprocess().call(cmd, close_fds=True, stderr=Msg.chlderr,
                                 stdout=Msg.chlderr) # call curl
        hdr.setvalue_from_file(self._files["header_file"])
        hdr.data["X-ND-CURLSTATUS"] = status
        if status:
            Msg().err("Error: in download: %s"
                      % str(FileUtil(self._files["error_file"]).getdata('r')))
            FileUtil(self._files["output_file"]).remove()
            return (hdr, buf)
        status_code = self.get_status_code(hdr.data["X-ND-HTTPSTATUS"])
        if "header" in kwargs:
            hdr.data["X-ND-HEADERS"] = kwargs["header"]
        if status_code == 401: # needs authentication
            pass
        elif 300 <= status_code <= 308: # redirect
            pass
        elif "ofile" in kwargs:
            if status_code == 206 and "resume" in kwargs:
                os.rename(self._files["output_file"], kwargs["ofile"])
            elif status_code == 416:
                if "resume" in kwargs:
                    kwargs["resume"] = False
                (hdr, buf) = self.get(self._files["url"], **kwargs)
            elif status_code != 200:
                Msg().err("Error: in download: ", str(
                    hdr.data["X-ND-HTTPSTATUS"]), ": ", str(status))
                FileUtil(self._files["output_file"]).remove()
            else:  # OK downloaded
                os.rename(self._files["output_file"], kwargs["ofile"])
        if "ofile" not in kwargs:
            try:
                buf = strio(open(self._files["output_file"], 'rb').read())
            except(IOError, OSError):
                Msg().err("Error: reading curl output file to buffer")
            FileUtil(self._files["output_file"]).remove()
        FileUtil(self._files["error_file"]).remove()
        FileUtil(self._files["header_file"]).remove()
        return (hdr, buf)
def get ( self , * args , * * kwargs ) : hdr = CurlHeader ( ) buf = strio ( ) self . _set_defaults ( ) cmd = self . _mkcurlcmd ( * args , * * kwargs ) status = Uprocess ( ) . call ( cmd , close_fds = True , stderr = Msg . chlderr , stdout = Msg . chlderr ) # call curl hdr . setvalue_from_file ( self . _files [ "header_file" ] ) hdr . data [ "X-ND-CURLSTATUS" ] = status if status : Msg ( ) . err ( "Error: in download: %s" % str ( FileUtil ( self . _files [ "error_file" ] ) . getdata ( 'r' ) ) ) FileUtil ( self . _files [ "output_file" ] ) . remove ( ) return ( hdr , buf ) status_code = self . get_status_code ( hdr . data [ "X-ND-HTTPSTATUS" ] ) if "header" in kwargs : hdr . data [ "X-ND-HEADERS" ] = kwargs [ "header" ] if status_code == 401 : # needs authentication pass elif 300 <= status_code <= 308 : # redirect pass elif "ofile" in kwargs : if status_code == 206 and "resume" in kwargs : os . rename ( self . _files [ "output_file" ] , kwargs [ "ofile" ] ) elif status_code == 416 : if "resume" in kwargs : kwargs [ "resume" ] = False ( hdr , buf ) = self . get ( self . _files [ "url" ] , * * kwargs ) elif status_code != 200 : Msg ( ) . err ( "Error: in download: " , str ( hdr . data [ "X-ND-HTTPSTATUS" ] ) , ": " , str ( status ) ) FileUtil ( self . _files [ "output_file" ] ) . remove ( ) else : # OK downloaded os . rename ( self . _files [ "output_file" ] , kwargs [ "ofile" ] ) if "ofile" not in kwargs : try : buf = strio ( open ( self . _files [ "output_file" ] , 'rb' ) . read ( ) ) except ( IOError , OSError ) : Msg ( ) . err ( "Error: reading curl output file to buffer" ) FileUtil ( self . _files [ "output_file" ] ) . remove ( ) FileUtil ( self . _files [ "error_file" ] ) . remove ( ) FileUtil ( self . _files [ "header_file" ] ) . remove ( ) return ( hdr , buf )