当前位置:  编程技术>python

Python开发实例分享bt种子爬虫程序和种子解析

    来源: 互联网  发布时间:2014-10-04

    本文导语:  看到网上也有开源的代码,这不,我拿来进行了二次重写,呵呵,上代码: 代码如下:    #encoding: utf-8      import socket      from hashlib import sha1      from random import randint      from struct import unpack, pack      from socket impor...

看到网上也有开源的代码,这不,我拿来进行了二次重写,呵呵,上代码:

代码如下:

    #encoding: utf-8 
    import socket 
    from hashlib import sha1 
    from random import randint 
    from struct import unpack, pack 
    from socket import inet_aton, inet_ntoa 
    from bisect import bisect_left 
    from threading import Timer 
    from time import sleep 
    import MySQLdb 
    from datetime import * 
    import time 
    from bencode import bencode, bdecode 
    BOOTSTRAP_NODES = [ 
        ("router.bittorrent.com", 6881), 
        ("dht.transmissionbt.com", 6881), 
        ("router.utorrent.com", 6881) 
    ]  
    TID_LENGTH = 4 
    KRPC_TIMEOUT = 10 
    REBORN_TIME = 5 * 60 
    K = 8 
    def entropy(bytes): 
        s = "" 
        for i in range(bytes): 
            s += chr(randint(0, 255)) 
        return s 
    def random_id(): 
        hash = sha1() 
        hash.update( entropy(20) ) 
        return hash.digest() 
    def decode_nodes(nodes): 
        n = [] 
        length = len(nodes) 
        if (length % 26) != 0:  
            return n 
        for i in range(0, length, 26): 
            nid = nodes[i:i+20] 
            ip = inet_ntoa(nodes[i+20:i+24]) 
            port = unpack("!H", nodes[i+24:i+26])[0] 
            n.append( (nid, ip, port) ) 
        return n 
    def encode_nodes(nodes): 
        strings = [] 
        for node in nodes: 
            s = "%s%s%s" % (node.nid, inet_aton(node.ip), pack("!H", node.port)) 
            strings.append(s) 
        return "".join(strings) 
    def intify(hstr): 
        return long(hstr.encode('hex'), 16)     
    def timer(t, f): 
        Timer(t, f).start() 
    class BucketFull(Exception): 
        pass 
    class KRPC(object): 
        def __init__(self): 
            self.types = { 
                "r": self.response_received, 
                "q": self.query_received 
            } 
            self.actions = { 
                "ping": self.ping_received, 
                "find_node": self.find_node_received, 
                "get_peers": self.get_peers_received, 
                "announce_peer": self.announce_peer_received, 
            } 
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
            self.socket.bind(("0.0.0.0", self.port)) 
        def response_received(self, msg, address): 
            self.find_node_handler(msg) 
        def query_received(self, msg, address): 
            try: 
                self.actions[msg["q"]](msg, address) 
            except KeyError: 
                pass 
        def send_krpc(self, msg, address): 
            try: 
                self.socket.sendto(bencode(msg), address) 
            except: 
                pass 
    class Client(KRPC): 
        def __init__(self, table): 
            self.table = table 
            timer(KRPC_TIMEOUT, self.timeout) 
            timer(REBORN_TIME, self.reborn) 
            KRPC.__init__(self) 
        def find_node(self, address, nid=None): 
            nid = self.get_neighbor(nid) if nid else self.table.nid 
            tid = entropy(TID_LENGTH) 
            msg = { 
                "t": tid, 
                "y": "q", 
                "q": "find_node", 
                "a": {"id": nid, "target": random_id()} 
            } 
            self.send_krpc(msg, address) 
        def find_node_handler(self, msg): 
            try: 
                nodes = decode_nodes(msg["r"]["nodes"]) 
                for node in nodes: 
                    (nid, ip, port) = node 
                    if len(nid) != 20: continue 
                    if nid == self.table.nid: continue 
                    self.find_node( (ip, port), nid ) 
            except KeyError: 
                pass 
        def joinDHT(self): 
            for address in BOOTSTRAP_NODES:  
                self.find_node(address) 
        def timeout(self): 
            if len( self.table.buckets ) < 2: 
                self.joinDHT() 
            timer(KRPC_TIMEOUT, self.timeout) 
        def reborn(self): 
            self.table.nid = random_id() 
            self.table.buckets = [ KBucket(0, 2**160) ] 
            timer(REBORN_TIME, self.reborn) 
        def start(self): 
            self.joinDHT() 
            while True: 
                try: 
                    (data, address) = self.socket.recvfrom(65536) 
                    msg = bdecode(data) 
                    self.types[msg["y"]](msg, address) 
                except Exception: 
                    pass 
        def get_neighbor(self, target): 
            return target[:10]+random_id()[10:] 
    class Server(Client): 
        def __init__(self, master, table, port): 
            self.table = table 
            self.master = master 
            self.port = port 
            Client.__init__(self, table) 
        def ping_received(self, msg, address): 
            try: 
                nid = msg["a"]["id"] 
                msg = { 
                    "t": msg["t"], 
                    "y": "r", 
                    "r": {"id": self.get_neighbor(nid)} 
                } 
                self.send_krpc(msg, address) 
                self.find_node(address, nid) 
            except KeyError: 
                pass 
        def find_node_received(self, msg, address): 
            try: 
                target = msg["a"]["target"] 
                neighbors = self.table.get_neighbors(target) 
                nid = msg["a"]["id"] 
                msg = { 
                    "t": msg["t"], 
                    "y": "r", 
                    "r": { 
                        "id": self.get_neighbor(target),  
                        "nodes": encode_nodes(neighbors) 
                    } 
                } 
                self.table.append(KNode(nid, *address)) 
                self.send_krpc(msg, address) 
                self.find_node(address, nid) 
            except KeyError: 
                pass 
        def get_peers_received(self, msg, address): 
            try: 
                infohash = msg["a"]["info_hash"] 
                neighbors = self.table.get_neighbors(infohash) 
                nid = msg["a"]["id"] 
                msg = { 
                    "t": msg["t"], 
                    "y": "r", 
                    "r": { 
                        "id": self.get_neighbor(infohash),  
                        "nodes": encode_nodes(neighbors) 
                    } 
                } 
                self.table.append(KNode(nid, *address)) 
                self.send_krpc(msg, address) 
                self.master.log(infohash) 
                self.find_node(address, nid) 
            except KeyError: 
                pass 
        def announce_peer_received(self, msg, address): 
            try: 
                infohash = msg["a"]["info_hash"] 
                nid = msg["a"]["id"] 
                msg = {  
                    "t": msg["t"], 
                    "y": "r", 
                    "r": {"id": self.get_neighbor(infohash)} 
                } 
                self.table.append(KNode(nid, *address)) 
                self.send_krpc(msg, address) 
                self.master.log(infohash) 
                self.find_node(address, nid) 
            except KeyError: 
                pass 
    class KTable(object): 
        def __init__(self, nid): 
            self.nid = nid 
            self.buckets = [ KBucket(0, 2**160) ] 
        def append(self, node): 
            index = self.bucket_index(node.nid) 
            try: 
                bucket = self.buckets[index] 
                bucket.append(node) 
            except IndexError: 
                return 
            except BucketFull: 
                if not bucket.in_range(self.nid): return 
                self.split_bucket(index) 
                self.append(node) 
        def get_neighbors(self, target): 
            nodes = [] 
            if len(self.buckets) == 0: return nodes 
            if len(target) != 20 : return nodes 
            index = self.bucket_index(target) 
            try: 
                nodes = self.buckets[index].nodes 
                min = index - 1 
                max = index + 1 
                while len(nodes) < K and ((min >= 0) or (max < len(self.buckets))): 
                    if min >= 0: 
                        nodes.extend(self.buckets[min].nodes) 
                    if max < len(self.buckets): 
                        nodes.extend(self.buckets[max].nodes) 
                    min -= 1 
                    max += 1 
                num = intify(target) 
                nodes.sort(lambda a, b, num=num: cmp(num^intify(a.nid), num^intify(b.nid))) 
                return nodes[:K] 
            except IndexError: 
                return nodes 
        def bucket_index(self, target): 
            return bisect_left(self.buckets, intify(target)) 
        def split_bucket(self, index): 
            old = self.buckets[index] 
            point = old.max - (old.max - old.min)/2 
            new = KBucket(point, old.max) 
            old.max = point 
            self.buckets.insert(index + 1, new) 
            for node in old.nodes[:]: 
                if new.in_range(node.nid): 
                    new.append(node) 
                    old.remove(node) 
        def __iter__(self): 
            for bucket in self.buckets: 
                yield bucket 
    class KBucket(object): 
        __slots__ = ("min", "max", "nodes") 
        def __init__(self, min, max): 
            self.min = min 
            self.max = max 
            self.nodes = [] 
        def append(self, node): 
            if node in self: 
                self.remove(node) 
                self.nodes.append(node) 
            else: 
                if len(self) < K: 
                    self.nodes.append(node) 
                else: 
                    raise BucketFull 
        def remove(self, node): 
            self.nodes.remove(node) 
        def in_range(self, target): 
            return self.min

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 基于python实现的网络爬虫功能:自动抓取网页介绍
  • 一则python3的简单爬虫代码
  • python网络爬虫采集联想词示例
  • python爬虫教程之爬取百度贴吧并下载的示例
  • Python天气预报采集器实现代码(网页爬虫)
  • python爬虫常用的模块分析
  • python抓取网页图片示例(python爬虫)
  • python使用rabbitmq实现网络爬虫示例
  • Python爬虫框架Scrapy安装使用步骤
  • python实现的一只从百度开始不断搜索的小爬虫
  • python实现博客文章爬虫示例
  • python实现爬虫下载漫画示例
  • python iis7站长之家
  • python模拟新浪微博登陆功能(新浪微博爬虫)
  • Python GUI编程:tkinter实现一个窗口并居中代码
  • 让python同时兼容python2和python3的8个技巧分享
  • Python不使用print而直接输出二进制字符串
  • 使用setup.py安装python包和卸载python包的方法
  • Python中实现json字符串和dict类型的互转
  • 不小心把linux自带的python卸载了,导致安装一个依赖原python的软件不能安装,请问该怎么办?
  • python异常信息堆栈输出到日志文件
  • python读取csv文件示例(python操作csv)
  • python下用os.execl执行centos下的系统时间同步命令ntpdate
  • python基础教程之python消息摘要算法使用示例
  • Python namedtuple对象json序列化/反序列化及对象恢复
  • 新手该如何学python怎么学好python?
  • Python获取网页编码的方法及示例代码
  • 使用python删除nginx缓存文件示例(python文件操作)
  • Python异常模块traceback用法举例
  • python学习手册中的python多态示例代码
  • python之平台独立的调试工具winpdb介绍




  • 特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3