验证Python中的torrent文件是否有效?

验证Python中的torrent文件是否有效?,python,bittorrent,torrent,Python,Bittorrent,Torrent,有人知道Python中检查torrent文件(即file.torrent)是否有效的方法吗? os.path.exists() 有什么想法吗 谢谢 更新1: 由于你们中的许多人发现上面的描述是一般性的,这里有一个更详细的描述 我使用请求库下载torrents,并将其放入客户端的watch文件夹中,然后客户端自动开始下载 def download_torrent(torrent_url, file_path, pause=None, verbose=True): """ Down

有人知道Python中检查torrent文件(即file.torrent)是否有效的方法吗?
os.path.exists()

有什么想法吗

谢谢

更新1:

由于你们中的许多人发现上面的描述是一般性的,这里有一个更详细的描述
我使用
请求
库下载torrents,并将其放入客户端的watch文件夹中,然后客户端自动开始下载

def download_torrent(torrent_url, file_path, pause=None, verbose=True):
    """ 
    Downloads a torrent file, if it doesn't already exist.

    PARAMETERS
    - torrent_url:    torrent url
    - file_path:      absolute local filepath
    - pause:          integer number of seconds
    - verbose:        True/False
    """
    import requests

    if not os.path.exists(file_path): # torrent file does not exist in location
        r = requests.get(torrent_url)
        filename = os.path.basename(file_path)
        with open(file_path, "wb") as torrent:
            if verbose: print "> Downloading '%s'..." %(os.path.basename(file_path))
            torrent.write(r.content)
        if pause != None: 
            sleep(pause)
    else: # torrent file exists already
        if verbose: 
            print "! '%s' already exists, skipping file..." %(os.path.basename(file_path))
这在大多数情况下都很好。但是,客户端无法加载某些torrent文件,因为它们已损坏。

我正在寻找一种方法来识别这些文件,从而防止客户端加载它们。

要验证torrent文件是否有效,您需要bencoder/decoder实现来解析文件,然后检查中的必填字段是否存在以及是否符合预期格式


提供python绑定。Bittornado应该包含一个纯python实现,尽管它有点过时。

如何定义有效的torrent文件?我想您可以使用
试试。。。除了库解析函数的
。问题太广泛了,请提供一个比较
.torrent文件和它的MD5校验和感谢@davedwards,我的问题似乎源于torrent客户端的不兼容性,例如传输,在torrent中包含非英语字符。