TinyP2P 世界最小的P2P应用

来源:互联网 发布:演唱会黄牛的票 知乎 编辑:程序博客网 时间:2024/05/06 04:25

The World's Smallest P2P Application
世界上最小的P2P应用

Written by Ed Felten, with help from Alex Halderman.


翻译 吴尔平
2005/10/25 创建
2009/05/10 从原来的站点移过来,加语法高亮 http://www.danbala.com/python/lpy/

   1    # tinyp2p.py 1.0 (documentation at http://freedom-to-tinker.com/tinyp2p.html)   2    import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac # (C) 2004, E.W. Felten   3    ar,pw,res = (sys.argv,lambda u:hmac.new(sys.argv[1],u).hexdigest(),re.search)   4    pxy,xs = (xmlrpclib.ServerProxy,SimpleXMLRPCServer.SimpleXMLRPCServer)   5    def ls(p=""):  return filter(lambda n:(p=="")or res(p,n),os.listdir(os.getcwd()))   6    if ar[2]!="client": # license: http://creativecommons.org/licenses/by-nc-sa/2.0   7      myU,prs,srv = ("http://"+ar[3]+":"+ar[4], ar[5:],lambda x:x.serve_forever())   8      def pr(x=[]):return ([(y in prs) or prs.append(y) for y in x] or 1) and prs   9      def c(n): return ((lambda f: (f.read(), f.close()))(file(n)))[0]  10      f=lambda p,n,a: (p==pw(myU))and(((n==0)and pr(a))or((n==1)and [ls(a)])or c(a))  11      def aug(u): return ((u==myU) and pr()) or pr(pxy(u).f(pw(u),0,pr([myU])))  12      pr() and [aug(s) for s in aug(pr()[0])]  13      (lambda sv:sv.register_function(f,"f") or srv(sv))(xs((ar[3],int(ar[4]))))  14    for url in pxy(ar[3]).f(pw(ar[3]),0,[]):  15      for fn in filter(lambda n:not n in ls(), (pxy(url).f(pw(url),1,ar[4]))[0]):  16        (lambda fi:fi.write(pxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc"))

TinyP2P is a functional peer-to-peer file sharing application, written in fifteen lines of code, in the Python programming language. I wrote TinyP2P to illustrate the difficulty of regulating peer-to-peer applications. Peer-to-peer apps can be very simple, and any moderately skilled programmer can write one, so attempts to ban their creation would be fruitless.

 

TinyP2P是一个仅用15行python代码完成的P2P文件共享应用。我写TinyP2P是用来说明限制P2P应用的难度。P2P应用可以非常简单,任何一个中等水平程序员都能写,所以试图去禁止他们的创造将是徒劳的。

(Each line has 80 characters or fewer. The first line doesn't count -- it's a label for human readers and is ignored by the computer.)

(每行80个字符或更少,第一行不计算在内--它只是给阅读者看的一个标注,计算机并不理会)

My goal in creating this program is not to facilitate copyright infringement. I do not condone copyright infringement. Nothing about the program's design is optimized for the sharing of infringing files. The program is useful mainly as a proof of concept. A more practical program would be faster, more secure, and more resilient against failure. But that would require a few more lines of code!

我创建这个程序的目的并不是为了方便侵权,我不宽容侵权,但对程序设计的优化与共享侵权文件是无关的。这个程序主要用来验证上面的观点(P2P程序易于编写)。一个实用的程序当然更快,更安全,失败处理更有弹性,但这也会需要更多行的代码!

The TinyP2P code is available for download at http://www.freedom-to-tinker.com/tinyp2p.py.

http://www.freedom-to-tinker.com/tinyp2p.py有可用的TinyP2P代码下载。

How It Works

它如何工作

The program creates a small-world network, which might be used by a group of friends or business associates to share files. The program does not work well for very large networks; instead, many small networks can coexist. Each network is protected by a password; a network can be accessed only by people who know its password. (But networks are not secure against attackers who can eavesdrop on their traffic.)

程序创建一个小规模的网络,一群朋友或商业伙伴可以用它共享文件。这个程序在非常大的网络不能很好的工作;作为可替代的,许多小网络能够共存。每一个网络都被密码保护;网络仅能被知道密码的人访问。(不过如果有攻击者能窃听通信则网络是不安全的。)

The program uses standard communication protocols: HTTP and XML-RPC. HTTP is the same protocol used by web browsers to fetch pages, and XML-RPC is widely used to provide web services.

程序使用标准通信协议:HTTP和XML-RPC。HTTP就是web浏览器获取页面时使用的协议,XML-RPC则在提供web服务时广泛使用。

The program can be run in one of two modes, server or client. When run as a server, the program connects to a network of other servers, and makes all of the files in the current directory available for downloading by users of the network. (Files deposited into that directory while the server is running will become available immediately to other users.) To run the program as a server, you type this command:

程序可以选择作为服务器模式或客户端模式运行。当运行在服务器模式,程序连接到其它服务器的网络,并使当前目录下所有文件对于网络中的用户下载有效。(当服务运行时,存放到目录的文件对其它的用户立即变得有效。)键入下面命令将程序运行在服务器模式:

python tinyp2p.py password server hostname portnum [otherurl]

Here password is the password for the network, hostname and portnum will be used to construct the URL on which the server will listen (http://hostname:portnum), and otherurl (which is optional) is the URL of another server that is already running as part of the network. (Otherurl will be used to hook up your server to the network. If you don't provide an otherurl, the program will assume that your server is starting a new network.)

这里password是网络密码,hostname和portnum被用来构建服务器将侦听的URL(http://hostname:portnum), otherurl(可选项)是其它已经作为网络一部分运行的服务器的URL.(otherurl用来将你的服务联接到网络,如果你不提供otherurl,程序会假设你的服务开始一个新的网络。)

To run the program as a client, you type this command:

键入下面命令将程序运行在客户端模式:

python tinyp2p.py password client serverurl pattern

Here password is the network's password, serverurl is the URL of a server that belongs to the network, and pattern is a character string. This command looks at every file being shared by every server on the network. A file is downloaded, and stored in the current directory, if two things are true: (a) the file's name contains the substring pattern, and (b) there is not already a file of the same name in the current directory. (Note for ubergeeks: pattern can actually be a Python regular expression, which is matched against filenames using Python's re.search library call.)

这里password是网络密码,serverurl是属于网络的服务器的URL,pattern是字符串。命令查看网络中每个服务器共享的文件。当以下两件事情为真时:(a)文件名中包含模式子串,(b)当前目录不存在同名文件,文件将被下载并存放在当前目录。(注意:模式实际上是Python正则式,能够被Python的re.search库用来匹配文件名。)

Note that if you run a client in the same directory where you're running a server, then files downloaded by the client will automatically be redistributed by the server. Depending on your circumstances, this may or may not be what you want. Also note that you can run multiple servers, belonging to different networks, in the same directory.

注意,如果你将客户程序与服务器程序运行在同一个目录,客户端下载的文件自动的被服务器程序重发布。依赖于你的环境,也许如你所想也许不。同样要注意的,你能在同一个目录运行多个属于不同网络的服务器。