[Tutor] Using sockets or telnetlib to finger?

Justin Sheehy dworkin@ccs.neu.edu
13 Feb 2000 21:48:51 -0500


Andr=E9 Dahlqvist <andre@beta.telenordia.se> writes:

> I myself prefer the telnetlib solution as it seams less "low-level", =
but
> since I'm brand new to Python I would like to hear what you gurus hav=
e to
> say about it.

I would say that either is fine.  If you prefer telnetlib, use 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:
>=20
> [zeus.kernel.org]
>=20
>     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-4
>=20
> 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=20=20
> method. What I do is that I split the string that contains this=20=20
> information with string.split(), and then access the version numbers=
=20
> directly in the resulting list using kernel_info[9], kernel_info[19] =
and
> kernel_info[28].

There are a number of ways.  If you are going to use string.split(),
you could first split on newlines and then split again, using the [-1]
index to get the last item from each line.

> 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?

One quick re-based solution could be something like:
re.findall('[\d\.\-]+', kernel_info)

> a_string =3D "can I do this to have a string \
> 	   continue on a second line?"

That depends on exactly what you mean by that statement.  That method
will let you compose strings over multiple lines, but the resulting
string will be a single line.

People generally use either use triple-quotes or escape codes.  That is:

>>> b =3D """this string occupies=20=20=20
... two lines"""

>>> c =3D "so does\nthis one"

I tend to use the latter more often, but it's a matter of style.

-Justin

=20


=20=20