【问题标题】:How to write a simple Bittorrent application?如何编写一个简单的 Bittorrent 应用程序?
【发布时间】:2011-07-21 00:37:11
【问题描述】:

如何编写一个简单的 bittorrent 应用程序。类似于使用 bittorrent 库的“hello world”,我的意思是理解 bittorrent 工作的最简单的应用程序。我更喜欢 python 或 C/C++ 实现,但它可以是任何语言。平台也不是问题,但我更喜欢 Linux。

对图书馆的建议,我已经从http://sourceforge.net/projects/bittorrent/develop下载了一个(我认为是官方的bittorrent)的源代码。但是,我在http://en.wikipedia.org/wiki/Comparison_of_BitTorrent_clients#Libraries 看到了很多其他库。我会很感激这方面的建议。

如果您只有一台笔记本电脑,如何测试应用程序。

【问题讨论】:

标签: python c network-programming p2p bittorrent


【解决方案1】:

你应该试试 libtorrent (rasterbar)。 http://libtorrent.org

如果你想用 python 编写你的客户端,在 linux 上,安装它:

sudo apt-get install python-libtorrent

一个非常简单的python代码示例,用它来下载种子:

import libtorrent as lt
import time
import sys

ses = lt.session({'listen_interfaces': '0.0.0.0:6881'})

info = lt.torrent_info(sys.argv[1])
h = ses.add_torrent({'ti': info, 'save_path': '.'})
s = h.status()
print('starting', s.name)

while (not s.is_seeding):
    s = h.status()

    print('\r%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d) %s' % (
        s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000,
        s.num_peers, s.state), end=' ')

    alerts = ses.pop_alerts()
    for a in alerts:
        if a.category() & lt.alert.category_t.error_notification:
            print(a)

    sys.stdout.flush()

    time.sleep(1)

print(h.status().name, 'complete')

【讨论】:

  • 哇。他们真正实现了“易于使用”的目标。这是一个很好的实现!
  • 你能告诉我这是做什么的吗? ses.listen_on(6881, 6891) 为什么是这些值?
  • 6881 是监听端口。如果绑定到该监听端口失败,libtorrent 将尝试将其加一并重试。如果它一直失败直到达到 6891,它将停止尝试并失败。错误报告为警报。
  • cl 参数应该使用什么(在sys.argv[1]?)我尝试了.torrent 文件的路径和磁力链接。
  • .torrent 文件的路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 2014-11-07
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 2013-08-01
相关资源
最近更新 更多