Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

python - How to write a simple Bittorrent application?

How to write a simple bittorrent application. Something like a "hello world" using a bittorrent library, I mean a simplest of the application to understand the working of bittorrent. I would prefer a python or a C/C++ implementation, but it can be any language. Platform is not an issues either, but i would prefer Linux.

Recommendations for the library to follow, I have downloaded the source code for one (i think official bittorrent) from - http://sourceforge.net/projects/bittorrent/develop. But, I see a lot of other libraries at http://en.wikipedia.org/wiki/Comparison_of_BitTorrent_clients#Libraries. I would appreciate recommendations on this.

How to test an application if all you have is one laptop.

question from:https://stackoverflow.com/questions/5400828/how-to-write-a-simple-bittorrent-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should try libtorrent (rasterbar). http://libtorrent.org

If you want to write your client in python, on linux, install it with:

sudo apt-get install python-libtorrent

A very simple example of python code to use it to download a torrent:

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('
%.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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...