• 2009-11-11

    我的第一个python脚本 - [python]

    呵呵,最近在学习python,只明白了大概,很多还不会,不熟悉。现在放一个python程序,目标是:遍历文件夹里的torrent文件,分析hash info 加密后是否跟原文件的文件名相同,不同的话,记录log文件。我写起来有点复杂,感觉还不是按照python的思维来写程序,要学的还很多。

    #! /usr/bin/python

    from sys import *
    from bencode import *
    import hashlib
    import os
    import os.path

    #-----test-----
    #if len(argv) != 2:
    #    print "ERROR: use ./33.py filename"
    #    exit(2)
    #file_name = argv[1]
    #-----test-----

    def check_torrent_hash(file_name):
        filename ='torrent/'+file_name
        metainfo_file = open(filename, 'rb')
        if metainfo_file:
            metainfo = bdecode(metainfo_file.read())
        else:
            print "ERROR: Not a valid torrent file"
            exit(1)
        metainfo_file.close()
        info = metainfo['info']
        hahaha =  bencode(info)
        mySha1 = hashlib.sha1()   
        mySha1.update(hahaha)
        mySha1_Digest = mySha1.hexdigest()
        up_str = mySha1_Digest.upper();
        OK = file_name.split('.')   
        #print up_str
        #print OK[0]
        if not up_str==OK[0]:
            error_file = open('/home/changyou/wget/error.log', 'a+')
            error_file.writelines(up_str+'\n')
            error_file.close()
            return 'NO~~please check /home/changyou/wget/error.log'
        else:
            return 'OK!!!'

    rootdir = "/home/changyou/wget/torrent/"
    for parent, dirnames, filenames in os.walk(rootdir):
        for filename in filenames:
            print check_torrent_hash(filename)


     

  • 2009-11-10

    python取BT种子的hashinfo - [python]

    折腾了1天,BT果然是变态,名副其实。

    BT是python编写,他的种子文件时以Bencoding编码格式编码,具体如下
    无论是web服务器提供的那个.torrent文件,还是peers和trackers进行的信息交互,都是基于Bencoding格式的。
    1 字符串:首先是字符串长度,然后是一个:号,接着是串内容,比如5:hello,就是指hello这个字符串。
    2 整数:以“i”开头(int的意思),然后是十进制数字,最后是一个“e”结尾,如i3e表示3,i-3e表示-3,i0e表示0,i-0e是非法的。
    3 列表:以“l”开头(list的意思),以“e”结尾。比如 l5:hello5:worldi1234ee 代表 ["hello","world",1234]
    4 字典:以“d”开头(dictionary),以“e”结尾。比如 d3:cow3:moo4:spam4:eggse,则代表{"cow":"moo", "spam","eggs"}

    这里,我们要取的info是种子文件里‘字典’info后面的所有(包括d,到最后的包括e),很长一串。

    最开始写了一个PHP脚本,只是分析的种子文件,只是把种子文件里的数据用数组的形式显,然后用函数sha1(),结果得到的hashinfo不正确。崩溃ing

    后来google了下,发现用python比较靠谱,因为有现成的模块,代码如下:

    #! /usr/bin/python
    ffrom sys import *
    from bencode import *
    import hashlib

    mySha1 = hashlib.sha1()  //新建对象
    if len(argv) != 2:
        print "ERROR: use ./hash_info.py filename"

        exit(2)
    filename = argv[1]
    metainfo_file = open(filename, 'rb')
    try:
        metainfo = bdecode(metainfo_file.read())
    except ValueError:
        print "ERROR: Not a valid torrent file"
        exit(1)
    metainfo_file.close()
    info = metainfo['info'] //取列表的info
    hahaha =  bencode(info) //取torrent的info

    mySha1.update(hahaha)
    mySha1_Digest = mySha1.hexdigest()
    print mySha1_Digest  //得到info sha加密后的数据!!

    ---------------------------------------------------------------------

    注意:在linux下自带的python里没有hashlib模块,和bencod模块

    需要下载安装,hashlib     http://code.krypto.org/python/hashlib/

    bencod需要下载BitTorrent源码,或者下载单独文件:http://www.pinking.net.cn/scrip/bencode.py,跟脚本程序放在同一目录下即可