验证 Python 中的 torrent 文件是否有效?
Verify if valid torrent file in Python?
有人知道 Python 中检查 torrent 文件(即 file.torrent)是否有效的方法吗?
os.path.exists()
如果您想知道文件是否存在于给定位置,效果很好,但我想检查现有文件本身是否是有效的 torrent。
有什么想法吗?
谢谢。
更新 1:
由于很多人觉得上面的描述很笼统,这里有一个更详细的描述!
我用 request
库下载种子,然后将它们放入客户端的 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 实现来解析文件,然后检查 BEP3 中的必填字段是否存在以及是否采用预期的形式。
libtorrent 提供 python 绑定。 Bittornado 应该包含一个纯粹的 python 实现,尽管它有点过时了。
有人知道 Python 中检查 torrent 文件(即 file.torrent)是否有效的方法吗?
os.path.exists()
如果您想知道文件是否存在于给定位置,效果很好,但我想检查现有文件本身是否是有效的 torrent。
有什么想法吗?
谢谢。
更新 1:
由于很多人觉得上面的描述很笼统,这里有一个更详细的描述!
我用 request
库下载种子,然后将它们放入客户端的 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 实现来解析文件,然后检查 BEP3 中的必填字段是否存在以及是否采用预期的形式。
libtorrent 提供 python 绑定。 Bittornado 应该包含一个纯粹的 python 实现,尽管它有点过时了。