Using sockets or telnetlib to finger?

André Dahlqvist andre at beta.telenordia.se
Sat Feb 12 05:52:12 EST 2000


Hello

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.

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?

Regards

André

===================================================================
André Dahlqvist <andre at beta.telenordia.se>
GnuPG Key ID:   0x70A2994A
Fingerprint:    E947 3297 331C CA30 5B88  EDF2 A830 3EBE 70A2 994A
===================================================================




More information about the Python-list mailing list