P2P application in 15 lines of Python posted on slashdot

Hi Everybody,
slashdot published an article on someone's
15 lines long Peer-2-Peer application

Another person followed up with a 9 line equivalent Perl code.

I wonder what an equivalent Ruby program would look like?

I do not care whether it 15 or 25 lines. Python program is not very readable.
Can Ruby variant be concise, yet more clear?

--Leo--

I was going to post the same message :slight_smile:
Good rubyists unite! (I'm not good :frowning: ). Let's write a short more
understandable program!

slonik AZ wrote:

slashdot published an article on someone's
15 lines long Peer-2-Peer application
P2P In 15 Lines of Code - Slashdot

Another person followed up with a 9 line equivalent Perl code.

I wonder what an equivalent Ruby program would look like?

I did this 9.5 hours ago. Compared to the python one it is not vulnerable to File stealing attacks (a client can request a file .../foobar and ~/foobar from the python server and will get it back AFAIK) and 6 lines long. It is however vulnerable to the DRb style ..instance_eval exploits. I will fix this shortly, but I might have to use 7 lines then.

p2p.rb (759 Bytes)

slonik AZ wrote:

Hi Everybody,
slashdot published an article on someone's
15 lines long Peer-2-Peer application
P2P In 15 Lines of Code - Slashdot

I am saddened that they failed to mention ducks. Ducks, or, more particularly, images of ducks, are the only legitimate payload in peer-to-peer networks these days. And, notice how I summarized this point in less than 15 lines of text. Christmas miracles abound, folks.

_why

Actually, someone answered with a web server written in only 3 lines of codes.
More info here: P2P In 15 Lines of Code - Slashdot

···

On Thu, 16 Dec 2004 08:53:49 +0900, Giovanni Intini <intinig@gmail.com> wrote:

Let's write a short more understandable program!

Florian Gross wrote:

slonik AZ wrote:

slashdot published an article on someone's
15 lines long Peer-2-Peer application
P2P In 15 Lines of Code - Slashdot

Another person followed up with a 9 line equivalent Perl code.

I wonder what an equivalent Ruby program would look like?

I did this 9.5 hours ago. Compared to the python one it is not vulnerable to File stealing attacks (a client can request a file ../foobar and ~/foobar from the python server and will get it back AFAIK) and 6 lines long. It is however vulnerable to the DRb style .instance_eval exploits. I will fix this shortly, but I might have to use 7 lines then.

Here we go. Thanks to Mauricio Fernández for helping out with cutting off a few important characters!

p2p.rb (775 Bytes)

_why

No matter how much you might be off topic, I always enjoy reading your
posts. Another miracle lurking in the dark, there.

yours,
kaspar

LOL. The Python example used quite a few core libraries, so I think
a one-line instantiation of WebBRICK would suffice for a web server
example :slight_smile:

Seriously, these golf competitions don't do much for me. Show me
formatted code that applies some interesting algorithms instead.
That's how you spar with other languages!

···

On Thu, 16 Dec 2004 10:11:15 +0900, Purple Meteor <purple.meteor@gmail.com> wrote:

On Thu, 16 Dec 2004 08:53:49 +0900, Giovanni Intini <intinig@gmail.com> wrote:
> Let's write a short more understandable program!
Actually, someone answered with a web server written in only 3 lines of codes.
More info here: P2P In 15 Lines of Code - Slashdot

On Thu, Dec 16, 2004 at 10:37:13PM +0900, Florian Gross scribed:

>
>I did this 9.5 hours ago. Compared to the python one it is not
>vulnerable to File stealing attacks (a client can request a file
>../foobar and ~/foobar from the python server and will get it back
>AFAIK) and 6 lines long. It is however vulnerable to the DRb style
>.instance_eval exploits. I will fix this shortly, but I might have to
>use 7 lines then.

Here we go. Thanks to Mauricio Fern?ndez for helping out with cutting
off a few important characters!

Nice use of drb.

I wonder how long the same program would be in ruby if it implemented
the "tinyp2p" protocol instead of using drb. :wink:

  -dave

···

--
work: dga@lcs.mit.edu me: dga@pobox.com
      MIT Laboratory for Computer Science http://www.angio.net/

Florian Gross wrote:

Here we go. Thanks to Mauricio Fernández for helping out with cutting off a few important characters!

Another new version, binary files can be transfered to and from Win32, you can run servers from behind Routers and you can list all the files on the specified network sorted by node and file name before downloading them. Still six lines.

p2p.rb (910 Bytes)

Michael DeHaan ha scritto:

LOL. The Python example used quite a few core libraries, so I think
a one-line instantiation of WebBRICK would suffice for a web server
example :slight_smile:

ruby -rwebrick -e 'WEBrick::HTTPServer.new(:DocumentRoot=>".").start'
but it is a oneliner in python too :wink:

Seriously, these golf competitions don't do much for me. Show me
formatted code that applies some interesting algorithms instead. That's how you spar with other languages!

def qs(l)
  return if (x,*xs=*l).empty?
  less, more = xs.partition{|y| y < x}
  qs(less) + + qs(more)
end

algoritm suggestions are welcome :slight_smile:

* gabriele renzi <rff_rff@remove-yahoo.it> [Dec 16, 2004 10:30]:

def qs(l)
return if (x,*xs=*l).empty?
less, more = xs.partition{|y| y < x}
qs(less) + + qs(more)
end

algoritm suggestions are welcome :slight_smile:

How about

def qs(l)
  l.sort!
end

;-),
  nikolai

···

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

I hope sort is not implemented as quicksort :wink:

Regards,

Brian

···

On Thu, 16 Dec 2004 18:51:38 +0900 Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

* gabriele renzi <rff_rff@remove-yahoo.it> [Dec 16, 2004 10:30]:
> def qs(l)
> return if (x,*xs=*l).empty?
> less, more = xs.partition{|y| y < x}
> qs(less) + + qs(more)
> end

> algoritm suggestions are welcome :slight_smile:

How about

def qs(l)
  l.sort!
end

;-),
  nikolai

--
Brian Schröder
http://www.brian-schroeder.de/

* Brian Schröder <ruby@brian-schroeder.de> [Dec 16, 2004 13:10]:

> How about

> def qs(l)
> l.sort!
> end

I hope sort is not implemented as quicksort :wink:

array.c:

static VALUE
sort_internal(ary)
    VALUE ary;
{
    qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE),
          rb_block_given_p()?sort_1:sort_2);
    return ary;
}

rb_ary_sort_bang(ary)
    VALUE ary;
{
    rb_ary_modify(ary);
    if (RARRAY(ary)->len > 1) {
        FL_SET(ary, ARY_TMPLOCK); /* prohibit modification during sort */
        rb_ensure(sort_internal, ary, sort_unlock, ary);
    }
    return ary;
}

Just hope your systems qsort() is any good,
  nikolai

···

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

Just hope your systems qsort() is any good,

uln% grep qsort util.h
void ruby_qsort _((void*, const int, const int, int (*)(), void*));
#define qsort(b,n,s,c,d) ruby_qsort(b,n,s,c,d)
uln%

Guy Decoux

* ts <decoux@moulon.inra.fr> [Dec 16, 2004 14:30]:

> Just hope your systems qsort() is any good,

uln% grep qsort util.h void ruby_qsort _((void*, const int, const int,
int (*)(), void*)); #define qsort(b,n,s,c,d) ruby_qsort(b,n,s,c,d)

Then hope Ruby's implementation of qsort is any good ;-),
  nikolai

···

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

So just out of interest, what kind of sort is ruby using?
(I could dig the sources, but I hope it will be an easy question for someone to
answer without spending too much time.)

Regards,

Brian

···

On Thu, 16 Dec 2004 23:09:43 +0900 Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

* ts <decoux@moulon.inra.fr> [Dec 16, 2004 14:30]:
> > Just hope your systems qsort() is any good,

> uln% grep qsort util.h void ruby_qsort _((void*, const int, const int,
> int (*)(), void*)); #define qsort(b,n,s,c,d) ruby_qsort(b,n,s,c,d)

Then hope Ruby's implementation of qsort is any good ;-),
  nikolai

--
Brian Schröder
http://www.brian-schroeder.de/

* Brian Schröder <ruby@brian-schroeder.de> [Dec 16, 2004 16:30]:

So just out of interest, what kind of sort is ruby using? (I could
dig the sources, but I hope it will be an easy question for someone to
answer without spending too much time.)

Seems to be your standard memory-swap optimized quicksort, using its own
stack (i.e. no recursion), much like the one in glibc,
  nikolai

···

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}