Ok, now that nobody's left I can talk to myself...
Anyway, TinyP2P is a peer-to-peer file sharing application, written in fifteen lines of code (written in Python).
Every line has 80 characters or less!
Quite amazing.
And if you've read this far then you probably want to see these 15 lines so here goes:
# tinyp2p.py 1.0 (documentation at http://freedom-to-tinker.com/tinyp2p.html)
import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac # (C) 2004, E.W. Felten
ar,pw,res = (sys.argv,lambda u:hmac.new(sys.argv[1],u).hexdigest(),re.search)
pxy,xs = (xmlrpclib.ServerProxy,SimpleXMLRPCServer.SimpleXMLRPCServer)
def ls(p=""):return filter(lambda n:(p=="")or res(p,n),os.listdir(os.getcwd()))
if ar[2]!="client": # license: http://creativecommons.org/licenses/by-nc-sa/2.0
myU,prs,srv = ("http://"+ar[3]+":"+ar[4], ar[5:],lambda x:x.serve_forever())
def pr(x=[]): return ([(y in prs) or prs.append(y) for y in x] or 1) and prs
def c(n): return ((lambda f: (f.read(), f.close()))(file(n)))[0]
f=lambda p,n,a:(p==pw(myU))and(((n==0)and pr(a))or((n==1)and [ls(a)])or c(a))
def aug(u): return ((u==myU) and pr()) or pr(pxy(u).f(pw(u),0,pr([myU])))
pr() and [aug(s) for s in aug(pr()[0])]
(lambda sv:sv.register_function(f,"f") or srv(sv))(xs((ar[3],int(ar[4]))))
for url in pxy(ar[3]).f(pw(ar[3]),0,[]):
for fn in filter(lambda n:not n in ls(), (pxy(url).f(pw(url),1,ar[4]))[0]):
(lambda fi:fi.write(pxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc"))
Update:
Somebody wrote an even shorter version in Perl.
This one is only 9 lines and doesn't use any external libraries!!
Check it out: http://ansuz.sooke.bc.ca/software/molester/
$p=shift;$a=shift;i(shift);socket S,2,1,6;bind S,&a($a);listen
S,5;$/=undef;while(@ARGV&&($_="$p $a f".shift)||accept(C,S)&&($_=
C){m!^(.*?) (.*?) ([e-i])([^/]*)/!s&&$1 eq$p&&&$3($2,$4,$');}sub e{open
F,'>',$_[1];print F $_[2];close F}sub f{&s($_,@_)for keys %k}sub
g{open(F,'<',$_[1])&&&s($_[0],$a,"e$_[1]",
h{&s($_[0],$_,'i')for keys %k}sub i{$k{$_[0]}=1}sub
a{$_[0]=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub
s{socket X,2,1,6;$w=shift;if(connect X,&a($w)){print X
"$p $_[0] $_[1]/$_[2]";close X}else{undef $k{$p}}}
Now you see why I love Perl...
0 comments:
Post a Comment