[Tutor] Re: Re: How to find out DNS ?

Derrick 'dman' Hudson dman@dman.ddts.net
Thu, 27 Jun 2002 15:19:29 -0500


--ikeVEW9yuYc//A+q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, Jun 27, 2002 at 03:04:08PM -0300, Marc wrote:
| Derrick 'dman' Hudson (dman@dman.ddts.net) wrote:
|=20
| > f =3D open( "/etc/resolv.conf" , "r" )
| > nameservers =3D []
| > for line in f.xreadlines() :
|=20
| Nice code Dman. But there are some lines I didnt understand.
| Why are you using xreadlines? What's wrong with good, old readlines?

readlines reads the whole file into one (potentially big) list in
memory at once.  xreadlines instead reads the lines one-by-one as you
ask for them.  If the file is large, readlines() will be slow and use
lots of memory, and could even DoS the system if the file is large
enough.  xreadlines doesn't have that potential problem.  (hmm,
actually, any line reading method could have that problem if a really
large file with no linebreaks are given.  oh well, deal with it if you
think you might get such data :-))  Even though I don't expect
anyone's resolv.conf to be large, there's no reason not to use
xreadlines.  (actually, if I was using 2.2, a file object is iterable
so there's no need to call any methods on it)

| >     line =3D line.strip()
| >     if line.startswith( "nameserver" ) :
| >         _ , ip =3D line.split()
|=20
| I did not understand the thing with _ . Can you explain?

The line looks like
    nameserver  192.168.0.154
I can split() it on the space and get back a 2-element list containing
"nameserver" and "192.168.0.154".  I can conveniently use tuple
unpacking to assign each element to a different name.  That's great,
but I really only want the "192.168.0.154" part of the data.  So
instead of trying to find a descriptive name for data I don't want, I
used '_' as the name.  That variable is never used anywhere, so it
really doesn't matter, except that syntactically and semantically it
is required on that line.  Any other name would have worked just as
well, but '_' is easy to type and doesn't bear any other meaning.
(however, if I was going to use it, I would choose a descriptive name
for it instead).

| Thanks in advance

You're welcome.

| and sorry If my questions are too dumb.
=20
Those sorts of questions are expected on a 'tutor' list :-).

-D

--=20

A wise servant will rule over a disgraceful son,
and will share the inheritance as one of the brothers.
        Proverbs 17:2
=20
http://dman.ddts.net/~dman/


--ikeVEW9yuYc//A+q
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj0bc1EACgkQO8l8XBKTpRTLBACgnSzaHX0b3VfDA5tdFS9ifg78
7ngAniXffnhn7FB5L6Ga9MzpbV8HaTby
=k/er
-----END PGP SIGNATURE-----

--ikeVEW9yuYc//A+q--