Follow Slashdot blog updates by subscribing to our blog RSS feed


Forgot your password?
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:
  • hrm.. (Score:5, Funny)

    by grub ( 11606 ) <> on Wednesday December 15, 2004 @05:01PM (#11096702) Homepage Journal

    Freedom to Tinker has written a 15 line P2P program in Python

    Does anyone have a .torrent link for it?
    • Re:hrm.. (Score:5, Funny)

      by Anonymous Coward on Wednesday December 15, 2004 @05:08PM (#11096805)
      it's 9 lines in perl....


      fjdohsb0y[tu34qtyjhq5ykl2yjh4u5iongvwy5iopy9uy7u 87 srthmn90;
      ps394b7-6vh0ae6se0n89789t75j890t67scj89 067j890e54t 890;
      5478n35890904hn907t85j90670w9-57-xfg90hjx;om nxrt9b n0ps54;
      s907n-679s-90xe54/6w547/7589vj0s9-78se-06 vw346hbn8 7n98i7eu6;
      463g3w6g6v6u45betyv4yc45y4y5s7n86uijy4 63q4awy;
      4w57ne568une5j7vc7uyh35xjh7z6jx5uj6ukdtm cdryjtu;
      68ne578iyjvyhdrc h6ug37eb64n84mw6m8 s4r6i4rt6;

      I was suprised it was so simple! i can not believe i did not think of it already!
      • Re:hrm.. (Score:3, Funny)

        by forkazoo ( 138186 )
        Fermat apparently had proved a theorem for an N-line PERL script P2P app (n!=2), but in his famous "Treatise on the PERL language," he scrawled in the margin:

        I have found a single line of PERL to do this, but it is too long to fit in this margin.
    • or in BASH: (Score:4, Funny)

      by Anonymous Coward on Wednesday December 15, 2004 @05:12PM (#11096858)
      #! /bin/bash

      cd /src/mldonkey/distrib/

      # --- start mldonkey ---
  • 15 Lines? 9 Lines.
    The python code has
    import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac
    The perl code puts multiple commands in one line.

    Those are both cheating. And not really 15 or 9 lines of code. How many lines of code are just alone? Using these upper level languages is not a good way to prove how simple these activates are because they use many complicated libraries preinstalled in the language. It is like saying I can write a webserver in 3 lines of code.
    • I think it depends on whether the libraries are commonly available and considered part of the language "framework". If they are then its not really cheating as the amount of work involved is installing the framework (which is necessary to do any development in the language) and writing 15 lines of code.

      Of course multiple commands on the same line is cheating, especially in perl where you have to know all the magic to make things compact.

    • by wwahammy ( 765566 ) on Wednesday December 15, 2004 @05:06PM (#11096777)
      The point is that a person/entity can create a P2P program with a very small amount of custom code. If someone is going to ban P2P for "inducing" copyright infringement, they'd look stupid for banning a program this small or they'd have to ban the libraries that are used too which is pretty unlikely.
    • by grub ( 11606 ) <> on Wednesday December 15, 2004 @05:06PM (#11096779) Homepage Journal

      A p2p app is pretty pointless without a network stack but no one counts that as part of the app or supporting code. Don't pick the nits too much.
    • by Abcd1234 ( 188840 ) on Wednesday December 15, 2004 @05:07PM (#11096793) Homepage
      Umm, you're missing the point. The fact is with commonly available tools, and I'd consider Perl and Python (or Java) with their massive stock libraries "commonly available", one can easily write a p2p app (heck, BitTorrent is written in Python, so I think it's a very valid example).

      Hell, by your logic, the following application:

      int main(int argc, char **argv)
      printf("Hello World");

      is cheating, since I'm using printf, and god knows how complicated that call is, not to mention all the code in the OS to make the text appear on stdout!
    • While what you say is true, unless your UpperlevelProgrammingLanguage is commonly available, there is a lot of complexity in getting said language to install and work on a large number of systems. Using standard high level languages with standard libraries does cut down on the work to make portable servers available to the masses.

      The length of code isn't the real issue here, it's the effort required in writing said server and making it available to others.
    • by dtolton ( 162216 ) * on Wednesday December 15, 2004 @05:09PM (#11096814) Homepage
      I think you are missing the point. The point isn't that you can write a library called p2p and write a a two line python program:

      import p2p

      the point is that using standard built in libaries of these languages you can build a fully working p2p system in a very short amount of code.

      I do agree though that 15 lines is a bit misleading, although the python program is not putting multiple statements on one line, you can't do that in python. Instead he has removed all whitespace, put all defs and the following body on one line etc.

      Again though, the point *isn't* trying to make a program that is highly "pythonic", it isn't. The point is that using basic libraries that have been in Python for years, you can roll a p2p server in 20 minutes.
      • ...although the python program is not putting multiple statements on one line, you can't do that in python.

        A little off topic, but yes, you can -- just put the semicolon in between:

        $ python
        >>> import os; os.listdir(os.getcwd())

        Also, the following is nothing else but the multiple statements carefully hidden by the tuple packing/unpacking:

        a,b,c = (blah,lambda x:blah,blah-blah)

        I agree with the main point, though.
        • I vaguely remember reading that you can put semicolons to do multiple statements, but because it's considered such poor python style I've never used it. However I stand corrected on that point.

          I disagree that tuple unpacking is multiple statements though. That's the equivalent to saying that the function call do_some_operations() is carefully hiding multiple statements. Underneath the hood of python yes, tuple unpacking is multiple statements, but in python itself its nothing more than a variable assign
        • A little off topic, but yes, you can -- just put the semicolon in between

          That won't work for indented blocks, however.

          Fortunately, exec comes to the rescue!
          exec "for i in range(2):\n__for j in range(2):\n____for k in range(2):\n______print i, j, k"
          (using __ instead of regular spaces since Slashdot's comment molester deletes spaces)

          Now any Python program can be expressed in one (long) line.
    • While I agree there is underlying code written in a multitude of different languages why does it not count?

      Maybe it speaks volumes about where our coding languages have gone but the fact that you can write it in 15 lines (or 9 depending on which language you prefer :) just goes to show how easy it really is to write a P2P app with today's modern languages and libraries.

      Or are you one of those people that wants to go back to punch card for the authentic programming experience?
    • by Johnathon_Dough ( 719310 ) on Wednesday December 15, 2004 @05:11PM (#11096837)
      So? Are they going to have to also make illegal import sys, os, SimpleXMLRPCServer, xmlrpclib, re, and hmac? Or are those going to be on there because they have functions necessary to the OS not related to P2P apps?

      I think the point he was trying to make, is that it is too late to ban. Yeah, it would have been more impressive if he had coded it out of 15 lines of 1's and 0's, however, I think showing that you can take a bunch of embedded functions, and write a script that acts as a P2P app is a worthwhile example.

    • Those are both cheating.

      Okay, here's p2p in two lines of perl:

      `wget k-gnutella/gtk-gnutella-0.92.1c.tar.gz`;
    • The problem with that comparison is that things like are standard libraries that are used by a very wide selection of programs. The 15 lines here are actually unique, meaningful code specific to the application's task. The libraries are available to everyone. Nobody counts the code in header files as their own.

      Your "web browser" example is just invoking another program, and so it doesn't do anything unique or application specific.

      So when he says "done in 15 lines of code", it means that all the prog
    • #!/UpperlevelProgrammingLanguage
      Import webserver
      Run Webserver
      What, like this?
      #!/usr/bin/env ruby
      require 'webrick'
      s = :Port => 80, :DocumentRoot => File.join(Dir.pwd, "htdocs")
      trap("INT") { s.shutdown }
    • by Rheagar ( 556811 ) on Wednesday December 15, 2004 @05:16PM (#11096907) Homepage
      It also uses files, which is totally cheating. Without fi.write(), this guy would have to do a lot more work to have the computer convert a virtual address into the a device real address and accessing the filesystem implementation specific rules to carry out the necessary data and metadata operations to complete the task. And thats just the half of it.
    • by Jerf ( 17166 ) on Wednesday December 15, 2004 @05:20PM (#11096947) Journal
      While what you say is true, I think you are partially missing the point, because what you say is part of the point.

      None of the components included in the Python program are specifically P2P. But each of these components are common, powerful, and widely available; almost every, if not every, mainstream language you can think to name has each of these libraries easily available. This isn't news to Prof. Felten, it's an integral part of his point: These readily available libraries, for which no reasonable grounds can be come up with to eliminate them, are trivially combined into a P2P program.

      It is not the same as "import webserver;".

      Moreover, there is nothing XML-RPC or HMAC specific about the code, really, and you can't ban all RPC libraries, all hashing libraries, etc.

      This isn't really a demonstration of the power of Python or anything, and I think Molester sort of misses that point, though turning it into a Perl Golf contest is cool and nerdy and all. (Besides, Pythonistas like me are generally not impressed with such hyper-concision, since one of the reasons we use Python is readability and maintainablity; as a game it is great fun though.) Prof. Felten's point needs to be understood more like an academic proof that a problem is intractable; reduce the problem to something like the halting problem with a 1-to-1 mapping, and you're done. Here, Ed Felten reduces "P2P" to (taking it generically) a language and OS (absolutely vital, can't be banned without banning computers entirely), networking/communication, a bit of string processing (re is convenient but any turing complete language can do that), and a hashing algorithm which probably isn't even vital to the process.

      The point is to show that at the core, P2P can't be banned because there really isn't a "P2P" technology, it is an incredibly simple and straightforward application of the basic capabilities of a computer and a network connection. It has already been shown a rough equivalent can be written in Perl, and any number of others will probably pop up now. Languages like C++ or Java probably can't get down to 10 lines, but they will still be simple programs as programs in those languages go.
    • I can write a webserver in 2 lines:


      See! Easy!
  • Anyone can write a P2P client, but who will you network with? Not very useful with the other P.
  • by stevenbdjr ( 539653 ) <> on Wednesday December 15, 2004 @05:02PM (#11096724) Homepage

    Do Perl developers have some kind of reverse size-compensation complex?

    Anything you can do I can do smaller?

  • by phunster ( 701222 ) on Wednesday December 15, 2004 @05:02PM (#11096729)
    P2P Does Not Break the Law
    People Do
    • by Anonymous Coward on Wednesday December 15, 2004 @05:09PM (#11096813)
      The point is that when copyright holders attempt to take action against those people, you all start crying "They're suing children!" So they sue the P2P networks instead, and you start crying "Unfair! Go sue the users!" So they...
    • Nuclear bombs don't kill people, people do.
    • 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 Anonymous Coward
      I love how Orrin Hatch, a huge proponent of the NRA and the belief that "guns don't kill people, people kill people" (which I agree with), is the biggest pusher of the Induce Act, which implies that people don't violate copyright, software does. I want to puke.
    • P2P is the marijuana of the 21st century in the sense that it is an activity done by millions of people who don't think there is anything wrong with doing it. And it has been (actually in this case, will be) made illegal by clueless legislators prompted by outlandish claims by business groups.
      Now you have a classic situation where a vague law can be focused on a large group of people for political reasons. Individuals can be selected either randomly or because of their unrelated political activities or
  • by Anonymous Coward on Wednesday December 15, 2004 @05:03PM (#11096735)
    ...but it only worked with 1 dimension. I'm working on scaling it up, but I'm worried it might get longer.
  • by QangMartoq ( 614688 ) <SearchingBearCub@gmail.cLIONom minus cat> on Wednesday December 15, 2004 @05:04PM (#11096750)
    As we saw when the courts tried to ban DeCSS, the code was printed onto t-shirts.

    I can see P2P becoming the next DeCSS in the eyes of the courts and receiving similar treatment.

    So when can I expect my shirt?

  • by Anonymous Coward on Wednesday December 15, 2004 @05:04PM (#11096752)
    I'm Matthew Skala, the author of MoleSter, and my name was spelled correctly in the item I submitted about this.
  • by Anonymous Coward on Wednesday December 15, 2004 @05:04PM (#11096757)
    I have just created a zero line P2P program which I have entitled "Walking to the Neighbor's House to Borrow a Movie".

    I could be evil and patent it, but I have decided to release it under the GPL.
    • I have discovered a truly marvelous demonstration of P2P that this margin is too narrow to contain.

      --Fermat's Second-to-Last Conjecture

    • I perfer my (Score:3, Funny)

      by geekoid ( 135745 )
      "Friend Gives me movie to return to rental place and I forgot to go to the rental place" method.

      I get more movies that way.
    • I've added some new features to your wonderful program, which I too am releasing under the GPL. I call it:

      "Breaking in to the Neighbor's House to steal a Movie".
  • by Anonymous Coward on Wednesday December 15, 2004 @05:05PM (#11096765)
    ...are dedicated to spyware if its anything like kazaa.
  • MoleSter? (Score:5, Funny)

    by Mad_Rain ( 674268 ) on Wednesday December 15, 2004 @05:08PM (#11096806) Journal
    From the webpage: Every time I look at the word "molester" my brain tries to parse it as "mole-ster" instead of the agentive of "to molest", and now I have an excuse to name a piece of software MoleSter, so I'm going to use it.

    I think that the RIAA and MPAA are going to get a lot of positive spin when people start reading that they're going after all the MoleSters on the Internets.
  • by Anonymous Coward on Wednesday December 15, 2004 @05:10PM (#11096822)
    ### ToDo: Write P2P app here
  • by vivin ( 671928 ) <> on Wednesday December 15, 2004 @05:11PM (#11096847) Homepage Journal
    But according to this article [] from a story [] that was posted on Slashdot yesterday:

    But if next July's anticipated Supreme Court ruling in the MPAA/RIAA vs Grokster/Streamcast goes in favour of the movie and music industries, the heat is going to be on any technology, no matter how benign the intentions of its developer, that nevertheless makes piracy possible.

    Which is rather stupid and obtuse. If you're trying to pioneer a novel way to transfer data, then it could be used for piracy. Anything that transfers bits and bytes around can be held liable. So setting this precedent is just PLAIN STUPID. How far will *AA go? Let's say this precedent had already been established... then they could go after Brian Cohen. They could hold him responsible for create an application "makes piracy possible, regardless of his benign intentions". This way the *AA could crush anything that they see as a potential threat.
  • ThinkGeek (Score:5, Funny)

    by Lord_Dweomer ( 648696 ) on Wednesday December 15, 2004 @05:12PM (#11096860) Homepage
    And cue ThinkGeek tshirt slashvertisement in 5...4...3...

  • I don't see the correlation. Were p2p be made illegal(or whatever ... it's hypothetical), it wouldn't really matter whether it was 15 lines or 15 thousand lines.
    • The only correlation is that the program is so simple in concept that it is essentially stupid to ban.

      The ultimate in stupidity along these lines is if the RIAA and MPAA got PROGRAMMING banned as an activity because programming languages and tools induces copyright infringement.
    • by Anonymous Coward
      The link is intent. The notion that the RIAA/MPAA have is that makers of P2P applications are doing something complex and difficult, and so banning P2P applications isn't a big deal--it's a specialized activity that's hard to do, and it's relatively easy to police, and to demonstrate intent on behalf of the programmer.

      The goal of showing tha P2P applications are easy to write and can be extremely small is to say that you really can't ban P2P without banning the ability to transfer data over the internet i
    • During prohibition, bottles of grape juice came with labels warning those who bought them not to do this, this, and this because that would turn it into wine, and wine is illegal. If you don't think it's morally wrong, and it's easy to do and hard for authorities to control, laws aren't going to stop you.

      It's not about legality, it's about enforcement. If p2p is THIS easy, it's gonna be hard to fine or imprison everyone who does it, ever. There's only limited amounts of man-power to spend fighting anything
    • That's where the T-shirts with code on them come into play.
  • by GillBates0 ( 664202 ) on Wednesday December 15, 2004 @05:14PM (#11096880) Homepage Journal
    Here's a one-line P2P application I wrote in whitespace []:

    /*following code does p2p transfer*/

  • P2P tattoo (Score:4, Interesting)

    by Lord_Dweomer ( 648696 ) on Wednesday December 15, 2004 @05:15PM (#11096882) Homepage
    So I know that you can encode entire programs on barcodes. Well, could someone have a tattoo with this barcode on it for the program and carry it with them wherever they go? Even a temporary tattoo would be cool. Wearable computing takes on a whole new meaning, plus, you'd never be without access to free media.

  • by d-man ( 83148 ) <{chris} {at} {}> on Wednesday December 15, 2004 @05:21PM (#11096964) Homepage
    ...that Perl is a true write-only language.
  • ...the T-shirt!
  • by smug_lisp_weenie ( 824771 ) <> on Wednesday December 15, 2004 @05:24PM (#11096994) Homepage
    Our lawyers are currently perfecting a new TinyLawsuit specifically to defeat your invention. You will like it- Only _10_ lines of legalese!

    The ball is now in your court, Mr. Felten!

    Regards, The RIAA/MPAA
  • by Dan East ( 318230 ) on Wednesday December 15, 2004 @05:33PM (#11097084) Journal
    TinyP2P requires you specify the server address and port. Um, how is this different then FTPing to a server? Or sending a file over some IM service? Or copying a file over a network share?

    I thought the real point of p2p, as in file sharing, was the ability to search many hosts for something, even though you do not know what hosts exist, ideally without even requiring a central server the hosts must register with.

    Dan East

    • TinyP2P requires you specify the server address and port. Um, how is this different then FTPing to a server? Or sending a file over some IM service? Or copying a file over a network share?

      But, does there exist an ftp server you can trust? Seems like all of them have a root exploit discovered periodically. Likewise with the IM programs. However, 15 lines of code is pretty easy to audit.
    • Go do some reading. Once you connect to one host, you can get files that any other connected host has on your "network". All of the connected peers share files. See? Peer to peer file sharing.
  • Peer to peer tech aside, we regulate far simpler actions every day, or at least attempt to. We have laws against shoplifting, far simpler than writing a 9 to 15 line program. We have laws against pedophiles seducing little boys on the internet. That, I assume, is less complicated than writing a 9 line program. These laws don't catch everyone, but they do serve as a deterrent.

    Why wouldn't laws against P2P apps be effective? I think that the recent crackdowns on file sharing HAS had a significant impact
  • #!/bin/bash /usr/bin/
  • and each line was over 5,000 characters long...
  • ugh (Score:5, Funny)

    by ProfKyne ( 149971 ) on Wednesday December 15, 2004 @05:38PM (#11097174)
    There goes my argument that Python promotes readable code....
  • Did this remind anybody the Therapists Jeopardy skit with a fake Sean Connery on SNL?
  • They'll just make it illegal to write and publish software without an engineering license. Afterall, no one but freaks does anything complicated and non-sensual for fun...

    Old timers tend to not understand the concept of collaborative coding. My dad is a good example. One time he just looked at me and said that who in their right mind would right a program and NOT charge for it?
  • Anyone remember The Product []? It's a 63.5 kb first person shooter. Pretty neat.

    Slashdot covered it [] a few months ago.
  • by markdj ( 691222 ) on Wednesday December 15, 2004 @05:42PM (#11097217)
    Both these programs remind me of the obfuscated C program contest of years ago. They put multiple lines of code on one text line and use cryptic variable names to save space. They try to be cute to be small. Let's see how small a program can really be written if it is written in a decent style understandable by all. Not everyone knows the more arcane python and perl syntax. But a competent programmer could decipher it if written in a good style. The comment that "any moderately skilled programmer can write one" is BS if it has to be written so cryptically that only the interpreter/compiler can decipher it.
  • by SmittyTheBold ( 14066 ) <[deth_bunny] [at] []> on Wednesday December 15, 2004 @06:25PM (#11097666) Homepage Journal
    Matthew Scala, a reader of Freedom to Tinker, has responded with the 9 line MoleSter, written in Perl.

    There have been discussions recently about potential employers doing a Google search on job applicants, so the way I see it Mr. Scala's either very smart or very stupid.

    Very stupid, for the fact a lot of searches will put "Matthew Scala" and "molester" together on the same page.

    Very smart, because this tactic will bury any evidence of his pedophilia under a pile of MoleSter links and pages.

  • by ColGraff ( 454761 ) < minus painter> on Wednesday December 15, 2004 @07:07PM (#11098029) Homepage Journal
    These mini-P2P programs may not be useful for serious media sharing, but I can think of a real-world application - distribution of banned text (articles, newspapers, etc.) in the PRC and other repressive states. The software's tiny - small enough that anyone with Python or Perl installed could just keep the program on a folded sheet of paper, type it in when they want to use it, and delete the program when they were done. If you got in trouble, just burn the paper.
  • 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! ;-)

    # 1.0 (documentation at

    import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac # (C) 2004, E.W. Felten

    ar, pw, res = (sys.argv, lambda[1],u).hexdigest(),
    pxy, xs = (xmlrpclib.ServerProxy, SimpleXMLRPCServer.SimpleXMLRPCServer)

    def ls(p=""):
    return filter(
    lambda n: (p == "") or res(p, n),

    if ar[2] != "client": # license:
    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.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 N3wsByt3 ( 758224 ) on Wednesday December 15, 2004 @08:43PM (#11098924) Journal
    I'ts all very insteresting to read about this, but I'm wondering... seems creating P2P is very simple, yet why takes it so long for people to take up the chalenge of making a 'small social network' P2P-system?

    Wasn't it on this veryè same slashdot, that not too long ago an article was mentioned about a site who was willing to pay a considerable amount of money to any coder willing to work on a OSS P2P system annex IM which was meant to be used in a small network of friends, and thus, below the radar of RIAA and co.

    I gather the author in question isn't very interested in money, at least compared to 'status among peers' (something that seems to be typical of good coders working on OSS projects, like Linus). But I can't imagine that NO coder (at least the last time I checked) has taken that offer up for 1000 bucks, while this one makes a simple P2P prog in a matter of days, just to prove a point, or even just for the fun of it.

    I'm not sure how it was called again, but some probably will remember and maybe post a link to, the only thing to find are skala-type dudes - and getting a nice bonus on top. Volunteers?

Make it myself? But I'm a physical organic chemist!