コード例 #1
0
def download(url, descpath, threadnum=15):
    '''Download file by m3u8-url'''
    try:
        urllist = paresUrl(url)
        if len(urllist) <= 0:
            return False

        threads = ThreadTool(threadnum)

        # Creat tmpdir
        path = getDirName(descpath)
        tmpPath = getDiffTmpPathName(path)
        if mkdirs(tmpPath) is False:
            return False

        # Progress
        progress = ProgressTool(len(urllist), 20)

        # Download files
        files = []
        for i, item in enumerate(urllist):
            filepath = tmpPath + '/' + str(i) + '.ts'
            files.append(filepath)
            threads.start(__threadfunc__, item, filepath, progress)
        threads.waitAll()

        # merger
        __merger__(files, descpath)
        shutil.rmtree(tmpPath)
        threads.close()
        return True
    except:
        shutil.rmtree(tmpPath)
        return False
コード例 #2
0
    def start(self,
              showProgress: bool = False,
              threadNum: int = 10) -> (bool, str):
        size = len(self.fileUrls)
        if size == 1:
            fileSize, parts, msg = self.__getOneUrlParts__(self.fileUrls[0])
        elif size > 1:
            fileSize, parts, msg = self.__getMoreUrlsParts__(self.fileUrls)
        else:
            return False, "Urls is empty."

        if msg != "":
            return False, msg

        self.maxSize = fileSize

        try:
            check = createEmptyFile(self.filePath, fileSize)
            if not check:
                return False, "Create file failed."

            if self.userProgress is not None:
                self.userProgress.setMaxNum(fileSize)

            # thread
            threads = ThreadTool(threadNum)
            fileSize, unit = unitFix(fileSize, Unit.BYTE, Unit.MB)

            # Progress
            progress = None
            if showProgress:
                progress = ProgressTool(fileSize, 15, unit=unit.name)

            for item in parts:
                threads.start(__downloadPartFile__, item, self, progress, unit,
                              3)
            results = threads.waitAll()
            threads.close()

            for item in results:
                if item[0] is False:
                    return False, "Some parts download failed." + item[1]
            return True, ""
        except Exception as e:
            return False, str(e)
コード例 #3
0
ファイル: serverHelper.py プロジェクト: lloesche/AIGPY
class ServerTool(object):
    def __init__(self,
                 rootDir,
                 cookieDir,
                 fileOf404=None,
                 scfamily=socket.AF_INET,
                 sctype=socket.SOCK_STREAM):
        """
        #Func    :   初始化     
        #Param   :   rootDir    [in] 根目录     
        #Param   :   cookieDir  [in] cookie目录     
        #Param   :   fileOf404  [in] 404文件        
        #Param   :   scfamily   [in] 网络family     
        #Param   :   sctype     [in] 网络类型       
        """
        rootDir.replace('\\', '/')
        cookieDir.replace('\\', '/')
        self.__sockHandle = None
        self.__sockFamily = scfamily
        self.__sockType = sctype
        self.__rootDir = rootDir + '/'
        self.__cookieDir = cookieDir + '/'
        self.__fileOf404 = fileOf404
        self.__requestFunc = None
        self.__listenThread = ThreadTool(1)
        self.__requestThread = ThreadPoolManger(5)
        self.__revieveLen = 1024

    def __requestThreadCall__(self, sock, addr):
        body = None
        request = sock.recv(self.__revieveLen)
        http_req = HttpRequest(self.__rootDir, self.__cookieDir,
                               self.__fileOf404)
        http_req.passRequest(request)

        if self.__requestFunc is not None:
            body = self.__requestFunc(http_req)
        sock.send(http_req.getResponse(body).encode('utf-8'))
        sock.close()

    def __listenThreadCall__(self):
        while True:
            sock, addr = self.__sockHandle.accept()
            self.__requestThread.addWork(self.__requestThreadCall__,
                                         *(sock, addr))
        self.start()

    def start(self,
              address,
              port,
              requestFuc=None,
              listenNum=10,
              recieveLen=1024):
        """
        #Func    :   启动
        #Param   :   address    [in] ip地址 
        #Param   :   port       [in] 端口号
        #Param   :   requestFuc [in] 响应函数,参数为httpRequest,返回body     
        #Param   :   listenNum  [in] 监听的数量
        #Param   :   recieveLen [in] 数据包的长度
        #Return  :   True/False   
        """
        try:
            self.stop()
            self.__revieveLen = recieveLen
            self.__requestFunc = requestFuc
            self.__sockHandle = socket.socket(self.__sockFamily,
                                              self.__sockType)
            self.__sockHandle.bind((address, int(port)))
            self.__sockHandle.listen(listenNum)
            self.__listenThread.start(self.__listenThreadCall__)
            return True
        except:
            return False

    def stop(self):
        if self.__sockHandle is None:
            return
        self.__listenThread.close()
        self.__requestThread.close()
        self.__sockHandle.close()