Re: New to Python: my impression v. Perl/Ruby

From: John J. Lee (jjl_at_pobox.com)
Date: 01/19/04


Date: 19 Jan 2004 13:21:15 +0000

Wayne Folta <wfolta@netmail.to> writes:
[...]
> So I decided this might be a nice exercise to try Python. And it went
> together very quickly using Python's POP3 package. Then I decided to
> try it in Ruby. I think one little portion of the code shows the
> difference between the two cultures.
>
> The problem is that the message is not properly terminated, so you
> need to time out and catch that timeout to realize, "hmmm, malformed".
> In Python I had:
>
> M = poplib.POP3 ('172.16.30.1') ;
> M.user ('foo') ;
> M.pass_ ('bar')
>
> num_mesgs = len (M.list ()[1])
> bad_mesgs = 0
>
> for msg in range (num_mesgs):
> try:
> M.retr (msg + 1)
> ... blah blah...
>
> How do I get it to time out after 5 seconds and how do I catch that?
> The online docs are pretty good, but I had to guess that POP3 was
> built on the socket package. Looking at socket's docs I found the

Well, that's not exactly a huge leap given any knowledge of how the
internet works. I'm not sure that Python's docs should be in the
business of educating people about that...

> proper command and exception. I had to include:
>
> socket.setdefaulttimeout (5.0)
>
> before the POP3 commands to set the default socket timeout to 5
> seconds. And
>
> except socket.timeout:
>
> is the proper way to catch the timeout. Both of these things are in
> the socket documentation.

...but the fact that there is no timeout support in client modules of
the socket module is a known bug:

http://www.python.org/peps/pep-0042.html
http://www.python.org/sf/723287

It's likely to be fixed in 2.4. Timeouts on sockets were only
introduced in Python 2.3 (though there was a third party library
around for a long time before that that retrofitted timeouts into
the standard library).

> Contrast this with Ruby. The Ruby docs are less complete, but they did
> mention that POP was subclassed from protocol and you'd have to look
> at protocol's source to see how it works. Looking through protocol, I
> figured out what to do and it was more elegant.

Well, if Python's standard library had decided to subclass everything
from some 'protocol' base class, the docs would indeed mention that
fact, since that's would be a fact about the Python standard library
rather than about networking in general. It didn't, so it doesn't.

> The protocol class had a read_timeout, but since Ruby's mantra might
> be said to be "Real OO", the POP code had been written such that you
[...]

In the absence of some explanation of what you mean by "Real OO", that
sounds trollish.

I don't see how a missing feature in Python reveals any difference in
culture between Python and Ruby, other than perhaps that the Ruby
people like implementation inheritance more than the people who
designed the Python standard library.

John



Relevant Pages

  • Re: New to Python: my impression v. Perl/Ruby
    ... After years and years of Perl use, I evaluated Ruby ... > and Python and have adopted Python. ... the socket module docs for the default timeout setting. ... Yes, that's a substantive difference in design, I agree ...
    (comp.lang.python)
  • Re: killing thread after timeout
    ... >> I am going to write python script which will read python>> command from socket, run it and return some values back to>> socket. ... The child processes can acceptincomming connections on its copy of the socket. ... and pipe the data to and from worker processes. ...
    (comp.lang.python)
  • Re: Sockets on Windows and Mac
    ... > I am new to Python and have been writing some socket based programmes ... > on Windows, however I am unable to get them to work ... > on Mac. ...
    (comp.lang.python)
  • Re: socket.makefile() buggy?
    ... Python has been around for a long time, so you should ask yourself how ... import socket ... while reads < toread and retrytime < 10: ... but the client is actually sending 120. ...
    (comp.lang.python)
  • socket timeouts
    ... I used a very similar code to send data over a socket. ... Now in Python ... use settimeout() instead of the select.selectcall. ... what platforms support the settimeoutmethod so I suppose all ...
    (comp.lang.python)