python bittorrent 使用与学习

时间:2022-09-18 02:52:57

更新于20171128

关于这个p2p的技术,阿里开源了蜻蜓,大家可以看看,感觉应该比这个要好用,而且文档也全。

前言

最近在学习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"