From 228714810a3870924b158c78b2f9fafa69ec12aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=BE=D0=B4=D0=B8=D0=BD=20=D0=A0=D0=BE?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD?= Date: Sun, 5 May 2019 09:56:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BD=D1=83=D0=B4=D0=B8?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5=20=D0=B7=D0=B0=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pyrrent2http/pyrrent2http.py | 60 ++++---------------------------- 1 file changed, 7 insertions(+), 53 deletions(-) diff --git a/lib/pyrrent2http/pyrrent2http.py b/lib/pyrrent2http/pyrrent2http.py index a87dc28..0070d36 100755 --- a/lib/pyrrent2http/pyrrent2http.py +++ b/lib/pyrrent2http/pyrrent2http.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # -*- coding: utf-8 -*- import os import chardet @@ -303,7 +302,6 @@ class TorrentFS(object): return self.files[name] file_ = self.__file_at_(index) self.files[file_.name] = file_ - #self.handle.set_piece_deadline(self.files[startIndex].startPiece, 50) self.setPriority(index, 1) return file_ @@ -435,7 +433,7 @@ def HttpHandlerFactory(): buf = create_string_buffer(chunk) else: buf = bytearray(chunk) - while chunk > 0: + while chunk > 0 and not self.server.root_obj.forceShutdown: if start_range + chunk > end_range: chunk = end_range - start_range if os.getenv('ANDROID_ROOT'): @@ -444,7 +442,7 @@ def HttpHandlerFactory(): buf = bytearray(chunk) try: if f.Read(buf) < 1: break - while self.server.root_obj.pause: + while self.server.root_obj.pause and not self.server.root_obj.forceShutdown: time.sleep(0.1) continue if os.getenv('ANDROID_ROOT'): @@ -523,7 +521,6 @@ class Pyrrent2http(object): self.config.bindAddress = bindAddress self.config.downloadPath = downloadPath self.config.idleTimeout = idleTimeout - #self.config.fileIndex = fileIndex self.config.keepComplete = keepComplete self.config.keepIncomplete = keepIncomplete self.config.keepFiles = keepFiles @@ -624,30 +621,21 @@ class Pyrrent2http(object): self.TorrentFS = TorrentFS(self, self.torrentHandle) except Exception as e: logging.error(e.args) - #_ = self.TorrentFS.file(self.config.fileIndex) name = self.TorrentFS.info.name() self.torrent_name = name.decode(chardet.detect(name)['encoding']) def startHTTP(self): - #def http_server_loop(listener, alive): - # while alive.is_set(): - # print('+++handle request+++') - # listener.handle_request() - # listener.server_close() - #self.main_alive = threading.Event() - #self.main_alive.set() logging.info('Starting HTTP Server...') handler = HttpHandlerFactory() + handler.protocol_version = 'HTTP/1.1' logging.info('Listening HTTP on %s...\n' % (self.config.bindAddress,)) host, strport = self.config.bindAddress.split(':') if len(strport) > 0: srv_port = int(strport) self.httpListener = ThreadingHTTPServer((host, srv_port), handler) self.httpListener.root_obj = self - #self.httpListener.timeout = 0.5 - #thread = threading.Thread(target = http_server_loop, args = (self.httpListener, self.main_alive)) - thread = threading.Thread(target = self.httpListener.serve_forever) - thread.start() + self.listener_thread = threading.Thread(target = self.httpListener.serve_forever) + self.listener_thread.start() def startServices(self): if self.config.enableDHT: @@ -757,8 +745,6 @@ class Pyrrent2http(object): def Status(self): info = self.TorrentFS.info - #tstatus = self.torrentHandle.status() - #tstatus = self.TorrentFS.handle.status() tstatus = self.torrentHandle.status() status = { @@ -814,35 +800,6 @@ class Pyrrent2http(object): } peers['peers'].append(pi) return peers - '''def stats(self): - status = self.torrentHandle.status() - dhtStatusStr = '' - if not status.has_metadata: - return - if self.config.showAllStats or self.config.showOverallProgress: - sessionStatus = self.session.status() - if self.session.is_dht_running(): - dhtStatusStr = ', DHT nodes: %d' % (sessionStatus.dht_nodes,) - errorStr = '' - if len(status.error) > 0: - errorStr = ' (%s)' % (status.error,) - logging.info('%s, overall progress: %.2f%%, dl/ul: %.3f/%.3f kbps, peers/seeds: %d/%d' % ( - str(status.state), - status.progress * 100, - float(status.download_rate)/1024, - float(status.upload_rate)/1024, - status.num_peers, - status.num_seeds - ) + dhtStatusStr + errorStr - ) - if self.config.showFilesProgress or self.config.showAllStats: - str_ = 'Files: ' - for i, f in enumerate(self.TorrentFS.files): - str_ += '[%d] %.2f%% ' % (i, f.Progress()*100) - logging.info(str_) - if (self.config.showPiecesProgress or self.config.showAllStats) and self.TorrentFS.lastOpenedFile != None: - self.TorrentFS.lastOpenedFile.ShowPieces() -''' def consumeAlerts(self): alerts = self.session.pop_alerts() for alert in alerts: @@ -860,7 +817,6 @@ class Pyrrent2http(object): if type(alert) == alert_type: return alert def loop(self): - #self.statsTicker = Ticker(30) self.saveResumeDataTicker = Ticker(5) time_start = time.time() while True: @@ -875,8 +831,6 @@ class Pyrrent2http(object): if os.getppid() == 1: self.forceShutdown = True time_start = time.time() - #if self.statsTicker.true: - # self.stats() if self.saveResumeDataTicker.true: self.saveResumeData(True) time.sleep(0.3) @@ -956,9 +910,9 @@ class Pyrrent2http(object): def shutdown(self): logging.info('Stopping pyrrent2http...') self.forceShutdown = True - #self.statsTicker.stop() self.saveResumeDataTicker.stop() self.httpListener.shutdown() + self.httpListener.socket.close() self.TorrentFS.Shutdown() if self.session != None: self.session.pause() @@ -968,5 +922,5 @@ class Pyrrent2http(object): self.saveSessionState() self.removeTorrent() logging.info('Aborting the session') - del self.session + self.session = None logging.info('Bye bye')