Using sockets or telnetlib to finger?

Malcolm Tredinnick malcolmt at smart.net.au
Sat Feb 12 06:50:15 EST 2000


On Sat, Feb 12, 2000 at 11:52:12AM +0100, André Dahlqvist wrote:
> I am working on my first Python program, which will be used to finger a
> server that reports the latest stable, development and pre-patch version of
> the Linux kernel. I have come up with two different solutions on how to
> finger the host, and since I'm new to Python I am not sure which of these
> approaches is preferred. My first attempt was to use sockets since
> that was what the finger demo that came with Python used:
> 
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> sock.connect(HOST, PORT)
> sock.send('\n')
> while 1:
>     kernel_info = sock.recv(1024)
>     if not kernel_info:
>         break
> 
> This solution seams to do the trick, but so does my next approach which 
> uses telnetlib to telnet in to the finger port:
> 
> telnet = telnetlib.Telnet(HOST, PORT)
> telnet.write('\n')
> kernel_info = telnet.read_all()
> telnet.close()
> 
> I myself prefer the telnetlib solution as it seams less "low-level", but
> since I'm just starting out with Python I would like to hear what you 
> guys have to say about it.

The second solution looks cleaner to me, too. Four short lines and it pretty
much simulates the way you would do it in real life. One has to think a bit
about whether the first solution does the job, whereas the second one is just
obvious.

> Whatever approach I use I then need to grab the three version numbers from
> the finger output that I have collected. The finger output will look like
> this, where only the version numbers change:
> 
> [zeus.kernel.org]
> 
>     The latest stable version of the Linux kernel is:     2.2.14
>     The latest beta version of the Linux kernel is:       2.3.43
>     The latest prepatch (alpha) version *appears* to be:  2.3.44-8
> 
> I need to access the version numbers alone, since I want to translate the
> rest of the text. To do this I have used what I think is an ugly  
> method. What I do is that I split the string that contains this  
> information with string.split(), and then access the version numbers 
> directly in the resulting list using kernel_info[9], kernel_info[19] and
> kernel_info[28]. It seams quiet safe to do it like this since the text,
> apart from the version numbers, pretty much never changes, but is 
> there perhaps a equally simple solution that is less ugly? Could regular
> expressions do the trick here, and if so can someone perhaps give an
> example of how I would use them in this situation?

Your split solution seems a reasonable way to handle a not particularly pretty
problem. Even a regular expression solution will have to make some assumptions
about the format of the output (e.g. that the only colons will be right before
the kernel version numbers). As an example of how re's may be used (although I
would stick to the string.split method, probably):

import re
data = re.split(r':\s+(\d+[.]\d+[.]\d+(?:-\d+)?)', kernel_info)
stable, beta, prepatch = data[1], data[3], data[5]

There are no doubt other reg-exps that make other assumptions about the
contents of kernel_info that would do the trick. However, I think that once
again it comes back to the fact that looking at the reg-exp I gave above does
not make it immediately clear what is happening. And it is probably slower
than the string.split method.

Cheers,
Malcolm

--
The sooner you fall behind, the more time you'll have to catch up.






More information about the Python-list mailing list