源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Python实现解析Bit Torrent种子文件内容的方法

  • 时间:2020-01-18 10:18 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python实现解析Bit Torrent种子文件内容的方法
本文实例讲述了Python实现解析BitTorrent种子文件内容的方法。分享给大家供大家参考,具体如下: 有很多种子文件,有时候记不清里面都是什么东西,又不想一个一个的拖放到迅雷或BT软件里头看, 上网查了一下Python的脚本,自己也稍微修改了一下,代码如下,粘贴到文本编辑器中: 保存成py后缀的,直接运行
import re
def tokenize(text, match=re.compile("([idel])|(/d+):|(-?/d+)").match):
 i = 0
 while i < len(text):
  m = match(text, i)
  s = m.group(m.lastindex)
  i = m.end()
  if m.lastindex == 2:
   yield "s"
   yield text[i:i+int(s)]
   i = i + int(s)
  else:
   yield s
def decode_item(next, token):
 if token == "i":
  # integer: "i" value "e"
  data = int(next())
  if next() != "e":
   raise ValueError
 elif token == "s":
  # string: "s" value (virtual tokens)
  data = next()
 elif token == "l" or token == "d":
  # Container: "l" (or "d") values "e"
  data = []
  tok = next()
  while tok != "e":
   data.append(decode_item(next, tok))
   tok = next()
  if token == "d":
   data = dict(zip(data[0::2], data[1::2]))
 else:
  raise ValueError
 return data
def decode(text):
 try:
  src = tokenize(text)
  data = decode_item(src.next, src.next())
  for token in src: # look for more tokens
   raise SyntaxError("trailing junk")
 except (AttributeError, ValueError, StopIteration):
  raise SyntaxError("syntax error")
 return data
if __name__ == "__main__":
#需要读取的文件名称放到这里
 data = open("The_Shawshank_Redemption.torrent", "rb").read()
 torrent = decode(data)
 myfile = file("testit.txt", 'w')
 a = u'文件名称'.encode('gbk')
 b = u'文件大小'.encode('gbk')
 print "%s /t %s /n" % (a,b)
 for file in torrent["info"]["files"]:
  print "%s /t %d Mb " % ("/".join(file["path"]), file["length"]/1024/1024)
  print "-----------------------------------------------------------------"

注意要保存成Utf-8格式的文件,不能使用ASCII编码格式保存,否则中文会乱码或无法编译 [b]PS:这里再为大家推荐一款相关的在线工具供大家参考使用:[/b] [b]在线BT种子torrent/磁性链接在线转换工具: [/b][url=http://tools.jb51.net/aideddesign/bt2mag]http://tools.jb51.net/aideddesign/bt2mag[/url] 更多关于Python相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/516.htm]Python文件与目录操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/672.htm]Python文本文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/809.htm]Python URL操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/663.htm]Python数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/642.htm]Python函数使用技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/636.htm]Python字符串操作技巧汇总[/url]》及《[url=http://www.1sucai.cn/Special/788.htm]Python编码操作技巧总结[/url]》 希望本文所述对大家Python程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部