Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
The Internet Programming Technology

P2P In 15 Lines of Code 418

nile_list writes "Edward Felten of the very fine Freedom to Tinker has written a 15 line P2P program in Python. From the post on Freedom to Tinker, "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." Matthew Scala, a reader of Freedom to Tinker, has responded with the 9 line MoleSter, written in Perl."
This discussion has been archived. No new comments can be posted.

P2P In 15 Lines of Code

Comments Filter:
  • Actually, yes. (Score:5, Informative)

    by Kozz ( 7764 ) on Wednesday December 15, 2004 @05:16PM (#11096904)
    It's commonly referred to as "golf". ;) http://www.perlmonks.org/index.pl?node=golf [perlmonks.org]
  • by Stween ( 322349 ) on Wednesday December 15, 2004 @05:29PM (#11097042)
    Of course P2P doesn't break the law. P2P has been around for years, long before illegal sharing of copyrighted content on P2P systems hit the limelight.

    Consider: Usenet, 1979.
  • by miltimj ( 605927 ) on Wednesday December 15, 2004 @05:49PM (#11097310)
    If you'd read Skala's website, you'd see he already addresses your weak argument:

    You're using Socket.pm, and it's huge, that's cheating!
    Read the fucking code. I'm only using Socket.pm for its defined constants (such as SOCK_STREAM). I could easily eliminate Socket.pm, and save probably another 20 bytes or so, by replacing those constants with the numbers they represent; I have not done so yet only for portability's sake - it might make my code Linux-specific and I'd like to avoid that.
  • Re:I can beat that (Score:2, Informative)

    by AbbyNormal ( 216235 ) on Wednesday December 15, 2004 @06:05PM (#11097474) Homepage
    Or 22 bucks at Netflix and you magically get 3 cds out at a time, delivered to your door, from a rather large library. Ultimate laziness.
  • by swiftstream ( 782211 ) on Wednesday December 15, 2004 @06:09PM (#11097510)
    Years ago? [ioccc.org]

    This year saw the 17th International Obfuscated C Code Contest. There's some fun stuff in there.
  • by dstone ( 191334 ) on Wednesday December 15, 2004 @07:14PM (#11098078) Homepage
    Code is left intact, but here is the whitespace massaged into a more widely-accepted (and readable) convention. You see, Python isn't -that- sensitive to whitespace! ;-)


    # 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"))
  • by Anonymous Coward on Thursday December 16, 2004 @09:59AM (#11103175)

    I think the Python solution has a security bug that allows attackers to retrieve all files that the user under which the server is run has access to by supplying a filename like "../foo". Here's a replacement version in Ruby:

    #!/usr/bin/ruby
    # 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,P,M,U,*O=File,Dir,*ARGV;def s(p)F.basename p[/\w.*/]end;def c u
    DRbObject.new((),u)end;def x(u);[P,u].hash;end;M["c"]?c(U).f(x(U)).map{|n|p=x n
    c=c n;(c.f(p,O[0],0).map{|f|s f}-D["*"]).map{|f|open(f,"w")<<c.f(p,f,1)}}:(DRb.
    start_service U,Class.new{def p(z=O)O.push(*z).uniq!;O;end;new.methods.map{|m|m[
    /_[_t]/]||private(m)};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).map{|u|c(u).f(x(u),p(U))rescue()};self; end}.new.y;sleep)

    I'm using DRb, a library for OOP-style remote procedure calls, instead of XML-RPC.

    In case this posting gets slashdotted or screwed up by the crazy space insertation engine the code is also available here. (with syntax highlighting!) [rafb.net]

    Oh, and for the trolls: I usually don't write obfuscated, golfed Ruby code, but the language certainly won't stop me from doing so when I need to do so for reasons of holy language wars.

"If it ain't broke, don't fix it." - Bert Lantz

Working...