| World's Shortest P2P app 
 
	 
	
		| Author | Message |   
		| Martin 
 
  
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 8:01 am    Post subject: World's Shortest P2P app |  |   
				| 
 |  
				| Weighing in at 15 lines of python code: 
 
 	  | code: |  	  | # 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"))
 | 
 
 http://www.freedom-to-tinker.com/tinyp2p.html
 
 Nifty, eh?
 
 And yes, I read slashdot.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 3:28 pm    Post subject: (No subject) |  |   
				| 
 |  
				| 6 lines of Ruby: 
 
 	  | code: |  	  | # Server: ruby p2p.rb password server server-uri merge-servers
# Sample: ruby p2p.rb foobar server druby://localhost:1337 druby://foo.bar:1337
 # Client: ruby p2p.rb password client server-uri download-pattern
 # Sample: ruby p2p.rb foobar client druby://localhost:1337 *.rb
 require'drb';F,D,C,P,M,U,*O=File,Class,Dir,*ARGV;def s(p)F.split(p[/[^|].*/])[-1
 ]end;def c(u);DRbObject.new((),u)end;def x(u)[P,u].hash;end;M=="client"&&c(U).f(
 x(U)).each{|n|p,c=x(n),c(n);(c.f(p,O[0],0).map{|f|s f}-D["*"]).each{|f|F.open(f,
 "w"){|o|o<<c.f(p,f,1)}}}||(DRb.start_service U,C.new{def f(c,a=[],t=2)c==x(U)&&(
 t==0&&D[s(a)]||t==1&&F.read(s(a))||p(a))end;def y()(p(U)+p).each{|u|c(u).f(x(u),
 p(U))rescue()};self;end;private;def p(x=[]);O.push(*x).uniq!;O;end}.new.y;sleep)
 | 
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Tony 
 
  
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 8:35 pm    Post subject: (No subject) |  |   
				| 
 |  
				| Ruby is awesome   
 why is there
  inside the Python syntax?  |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 8:38 pm    Post subject: (No subject) |  |   
				| 
 |  
				| Because Martin didn't disable graphical smileys when he posted.   
 And the syntax tag doesn't wrap code in  tags, like it should.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| rizzix 
 
 
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 11:21 pm    Post subject: (No subject) |  |   
				| 
 |  
				| the problem here wtd is that ruby uses a semicolon to denote an end of statement. hence u cant really compare. |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Martin 
 
  
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 11:22 pm    Post subject: (No subject) |  |   
				| 
 |  
				| Who edited my post? Grr... |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 11:27 pm    Post subject: (No subject) |  |   
				| 
 |  
				| rizzix wrote: the problem here wtd is that ruby uses a semicolon to denote an end of statement. hence u cant really compare. 
 Not Ruby's fault that Python syntax can't do the same.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| rizzix 
 
 
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 11:28 pm    Post subject: (No subject) |  |   
				| 
 |  
				|     
 edit: well u could still compare # of statements.
 just wait till i post my perl code.. pfft. pwnage..
 
 edit:edit: i can't find it darn it
  |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| wtd 
 
 
 
 
 | 
			
				|  Posted: Fri Jan 07, 2005 11:45 pm    Post subject: (No subject) |  |   
				| 
 |  
				| No semi-colons, whiner, and slightly cleaned up.   
 And it's 11 lines, sans comments.
 
 
 	  | code: |  	  | require'drb'
P, M, U, *O = ARGV
 def s(p) File.split(p[/[^|].*/]).last end
 def c(u) DRbObject.new((), u) end
 def x(u) [P, u].hash end
 M == "client" && c(U).f(x(U)).each { |n|  p = x(n) and c = c(n) and (c.f(p, O[0], 0).map { |f| s f } - Dir["*"]).each { |f| File.open(f, "w") { |o| o << c.f(p, f, 1) } } } || (DRb.start_service U, Class.new {  def f(c, a = [],t = 2)  c == x(U) && (t == 0 && Dir[s(a)] || t == 1 && File.read(s(a)) || p(a))  end
 def y()  (p(U) + p).each { |u| c(u).f(x(u), p(U)) rescue() }
 self end
 private
 def p(x = []) O.push(*x).uniq! or O end }.new.y
 sleep)
 | 
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		|  |  
 |