2011-03-23 51 views
31

如何編寫一個簡單的bittorrent應用程序。像使用BitTorrent庫的「hello world」,我的意思是最簡單的應用程序來理解bittorrent的工作。我更喜歡Python或C/C++實現,但它可以是任何語言。平臺也不是問題,但我更喜歡Linux。如何編寫簡單的Bittorrent應用程序?

關於圖書館的建議,我已經從 - http://sourceforge.net/projects/bittorrent/develop下載了一個源代碼(我認爲是官方的bittorrent)。但是,我看到很多其他庫在http://en.wikipedia.org/wiki/Comparison_of_BitTorrent_clients#Libraries。我會很感激這方面的建議。

如何測試一個應用程序,如果你只有一臺筆記本電腦。

+3

閱讀規範(http://wiki.theory.org/BitTorrentSpecification),編寫代碼:-) – 2011-03-23 05:03:28

回答

73

你應該嘗試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() 
ses.listen_on(6881, 6891) 

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

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

    state_str = ['queued', 'checking', 'downloading metadata', \ 
     'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume'] 
    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, state_str[s.state]), 
    sys.stdout.flush() 

    time.sleep(1) 

print h.name(), 'complete' 
+1

哇。他們真的實現了「易於使用」的目標。這是一個很好的實現! – 2012-03-09 14:53:00

+0

你能告訴我這是幹什麼的嗎? 'ses.listen_on(6881,6891)'爲什麼這些值? – Gerep 2014-08-01 20:55:14

+0

6881是監聽端口。如果綁定到該監聽端口失敗,libtorrent會嘗試將其加1,然後重試。如果它一直失敗,直到它達到6891,它會停止嘗試,只是失敗。錯誤報告爲警報。 – Arvid 2014-08-01 23:17:41