python 网络(三、P2P Programming Framework)

       这篇文章纯粹是对http://cs.berry.edu这个大学的这个计算机的这段p2p框架代码的学习。代码结构灰常简单,逻辑清晰。并且基于这段代码实现了一个p2p文件共享系统。受益颇多。

一、p2p编程框架(及其python实现)

      

p2p框架代码: btpeer.py

p2p协议实现代码(即P2P文件共享协议): btfiler.py

P2P文件共享系统用户界面:filergui.py

二、

关于这个P2P框架,思考下面的问题

1. 为啥self.sd = self.s.makefile('rw',0),为何不直接在socket上读写

2. 语法?
try:
    ...
except KeyboardInterrupt:
    raise
except:
    if self.debug:
        traceback.print_exc()
    return False
return True

3. self.sd.read(4):表示最多读取4个byte,若不到4个就少读一点罢了
4. BTPeerConnection::recvdata()中,
    data = self.sd.read( min(2048, msglen-len(msg)) )
    (1) 2048用意?
    (2) self.sd.read(...)表示:最"多"读取的字节数

5. stabilizer的: 定期去ping每个peer, 将死亡的peer从self.peers中移除
(注:self.peerlock需要加锁)

参考文献

p2p框架参考:

Peer-to-Peer Programming                              http://cs.berry.edu/~nhamid/p2p/index.html

P2P Programming Framework - Python           http://cs.berry.edu/~nhamid/p2p/framework-python.html

File Sharing Application - Python                     http://cs.berry.edu/~nhamid/p2p/filer-python.html

编程参考:

socket模块                                                       http://docs.python.org/2/library/socket.html

threading模块                                                  http://docs.python.org/2.7/library/threading.html

python实现一个P2P文件发布,这段代码还没看过:                  http://blog.netzhou.net/?p=252

猜你喜欢

转载自chuanwang66.iteye.com/blog/2003932