当前位置: 首页>>代码示例>>Python>>正文


Python Session.serve_torrent_by_hash方法代码示例

本文整理汇总了Python中session.Session.serve_torrent_by_hash方法的典型用法代码示例。如果您正苦于以下问题:Python Session.serve_torrent_by_hash方法的具体用法?Python Session.serve_torrent_by_hash怎么用?Python Session.serve_torrent_by_hash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在session.Session的用法示例。


在下文中一共展示了Session.serve_torrent_by_hash方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Cloud

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import serve_torrent_by_hash [as 别名]
class Cloud(object):
    """
    Put or get a file from the Cloud.  This class maintains the session, which includes all files that we're serving up to other clients.
    
    This class is reponsible for "put"-ting and "get"-ting files to and from the cloud.
    """
    
    def __init__(self, torr_dir="c:/torrent", data_dir="c:/torrent/files", tracker_ip="10.0.0.1:8000", callback=lambda: ".", session_dir="c:/torrent", db_name="torrent.db", ext=".torrent"):
        """
        Intialize class.
        
        Parameters
        torr_dir: Location for torrent meta data files
        data_dir: Location for data files
        tracker_ip: IP:PORT string of tracker
        callback: option function which will be called while attempting to seed torrents.
        torr_db: Name of anydbm database where we save what we're serving.
        """        
        
        self.callback = callback
        self.data_dir = data_dir
        self.torr_dir = torr_dir
        self.ext = ext
        
        self.torr_db = os.path.join(session_dir, db_name)
        self.db = None                 # pointer to our torrent DB
        
        self.my_tracker = Tracker(tracker_ip)
        self.session = Session(session_dir, db_name)
        self.session.register(self.callback)
            
    def put(self, backup_file):
        """
        Put files to the tracker, given an encrypted ZIP archive, and then serve them.
        
        Parameters
        backup_file: Takes either a full path, or the basename (assumes a directory of self.data_dir in the latter case)
        """
        
        log.debug("backup_file = %s, data_dir = %s, torr_dir = %s, tracker = %s" % (backup_file, self.data_dir, self.torr_dir, self.my_tracker.tracker_ip))        
        
        # setup our TorrentMetaInfo object and create a torrent
        ti = TorrentMetaInfo(self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, os.path.basename(backup_file))
        
        # Make the torrent file and save new TorrentMetaInfo object with torrent name
        if ti.is_valid_create_object():        
            t=CreateTorrent(ti)
            ti = t.create(self.my_tracker)
        else:
            raise Exception("TorrentMetaInfo object is not valid for CreateTorrent.")
        
        # Upload torrent to tracker.  Return false if the upload fails.
        if not self.my_tracker.upload_torrent(ti):
            log.debug("Failed to upload torrent to tracker.")
            return False

        # Serve the torrent
        return self.session.serve(ti)
    
    def get(self, torr_hash_l):
        """
        Pass a torrent info hash, download files for torrent.
        
        Parameters
        torr_hash_l: Either a string or a list.  Right now, it only takes a single info hash of the torrent to pull down from the tracker.  The
                        data files associated with the torrent are then downloaded from the cloud.
        """
                
        if self.get_torrents(torr_hash_l):
            self.get_files(torr_hash_l)
            return True
        else:
            return False
            
    def get_files(self, torr_hash_l):
        """
        Download our files, given a torrent's info hash (string) or a list of torrent info hashes (strings).  Used by the get() function to pull down data
        files from the cloud.
        
        Parameters
        torr_hash_l: string, or list of strings 
        """
        self.session.serve_torrent_by_hash(torr_hash_l, self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, self.ext)
        
    def serve_torrents(self):
        """
        Serve up all torrents in our torr directory.  Does not take any parameters, simply checks for all torrents
        in our torrent meta data directory, and serves them.
        """
        self.session.serve_all_torrents(self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, self.ext)
               
    def get_torrents(self, torr_hash_l):
        """
        Pull a torrent down from the tracker by ID.
        
        Parameter 
        torr_hash_l: list or string of info hashes of torrents to download from the tracker.
        """
        
        flag = 0
#.........这里部分代码省略.........
开发者ID:jfmatth,项目名称:FishtankClient,代码行数:103,代码来源:cloud.py


注:本文中的session.Session.serve_torrent_by_hash方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。