python bittorrent 使用與學習


前言

最近在學習python的p2p協議,發現網上找不到好的文章,又仔細找了找終於找到了武老師之前講的視頻,自己動手做了一下,其實也不太難,不過其中有些地方還是沒搞懂。這里就先簡單做個筆記吧。

基本環境信息

兩台linux虛擬機

node1 192.168.3.3
node2 192.168.3.4

基本環境是一樣的

[root@node1 dist]# python --version
Python 2.7.5
[root@node1 dist]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

部署

本文大部分python操作都是在這個目錄下/application/bit/murder/dist/

兩台都要做的操作:

mkdir -p /application/bit
cd /application/bit
git clone https://github.com/WisWang/murder #這個是github twitter開源的一個項目,我fork到了我的github上面

准備一個tar文件,不要太小,放到當前目錄:

cd /
tar cf deploy.tar var
cd /application/bit/murder/dist/
mv /deploy.tar .

啟動tracker:

[root@node1 dist]# python murder_tracker.py

現在node1上面做種子:

cd /application/bit/murder/dist/
python murder_make_torrent.py deploy.tar 192.168.3.3:8998 deploy.torrent

然后啟動seed:

python murder_client.py seed deploy.torrent deploy.tar 192.168.3.3

拷貝種子文件到node2(種子文件應該放到http server或者ftp server上,這里就省略了):

[root@node2 dist]# pwd
/application/bit/murder/dist
[root@node2 dist]# scp 192.168.3.3:$PWD/deploy.torrent .

分別在node1和node2上面啟動peer,

[root@node1 dist]# python murder_client.py peer deploy.torrent deploy_new.tar 192.168.3.3

[root@node2 dist]# python murder_client.py peer deploy.torrent deploy_new.tar 192.168.3.4

一會兒就能看到這個deploy_new.tar下載完成了。
下載完成和做seed的時候都會看到 done and done這個信息

[root@node2 dist]# python murder_client.py peer deploy.torrent deploy_new.tar 192.168.3.4
done and done

下面是tracker的log供大家參考:

[root@node1 dist]# python murder_tracker.py
192.168.3.3 - - [08/May/2017:07:43:00] "GET /announce?info_hash=%FE%CE%A6%C6%28%0B%AB%DEve%00%8B%19%D1%80%A5%8A%DD%A3%D8&peer_id=T03H-----OQSfu8jzTCo&port=13159&uploaded=0&downloaded=0&left=0&no_peer_id=1&compact=1&event=started&ip=192.168.3.3&key=MCe15v HTTP/1.1" 200 60 "-" "BitTornado/T-0.3.17"
192.168.3.4 - - [08/May/2017:07:43:12] "GET /announce?info_hash=%FE%CE%A6%C6%28%0B%AB%DEve%00%8B%19%D1%80%A5%8A%DD%A3%D8&peer_id=T03H-----NwZ3DidklAM&port=15595&uploaded=0&downloaded=0&left=0&no_peer_id=1&compact=1&event=started&ip=192.168.3.4&key=vBp8fR HTTP/1.1" 200 67 "-" "BitTornado/T-0.3.17"
192.168.3.3 - - [08/May/2017:07:43:16] "GET /announce?info_hash=%FE%CE%A6%C6%28%0B%AB%DEve%00%8B%19%D1%80%A5%8A%DD%A3%D8&peer_id=T03H-----OQSfu8jzTCo&port=13159&uploaded=0&downloaded=0&left=0&no_peer_id=1&compact=1&ip=192.168.3.3&key=MCe15v HTTP/1.1" 200 67 "-" "BitTornado/T-0.3.17"
192.168.3.4 - - [08/May/2017:07:43:27] "GET /announce?info_hash=%FE%CE%A6%C6%28%0B%AB%DEve%00%8B%19%D1%80%A5%8A%DD%A3%D8&peer_id=T03H-----NwZ3DidklAM&port=15595&uploaded=0&downloaded=0&left=0&no_peer_id=1&compact=1&ip=192.168.3.4&key=vBp8fR HTTP/1.1" 200 67 "-" "BitTornado/T-0.3.17"
192.168.3.3 - - [08/May/2017:07:43:31] "GET /announce?info_hash=%FE%CE%A6%C6%28%0B%AB%DEve%00%8B%19%D1%80%A5%8A%DD%A3%D8&peer_id=T03H-----OQSfu8jzTCo&port=13159&uploaded=0&downloaded=0&left=0&no_peer_id=1&compact=1&ip=192.168.3.3&key=MCe15v HTTP/1.1" 200 67 "-" "BitTornado/T-0.3.17"
192.168.3.4 - - [08/May/2017:07:43:42] "GET /announce?info_hash=%FE%CE%A6%C6%28%0B%AB%DEve%00%8B%19%D1%80%A5%8A%DD%A3%D8&peer_id=T03H-----NwZ3DidklAM&port=15595&uploaded=0&downloaded=0&left=0&no_peer_id=1&compact=1&event=stopped&ip=192.168.3.4&key=vBp8fR HTTP/1.1" 200 54 "-" "BitTornado/T-0.3.17"

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com