(slightly OT): Python and linux - very cool

TuxTrax TuxTrax at fortress.tuxnet.net
Thu Aug 1 11:05:54 CEST 2002


I have some advocacy to do on the many wonderful tools that Linux
provides. While it is true that these tools are often available on
other platforms, they just *seem* to work more smoothly in linux, and
are alot easier to learn. I don't know. maybe it's just me. But Python
is a good example: I first learned of python in my windows days. Back
then, if it didn't have a GUI frontend, I wasn't interested. Geeze, I
should have known better; it wasn't like I was still wet behind the
ears. But I really didn't like IDLE, and soon lost interest in Python.

I don't know exactly why, but I don't take much to Idle, pythons
gui. It seems to get in the way. It's alot easier to fire up emacs
(since emacs has a python mode and makes python programming a snap)
from an xterm and away I go. Now it could be argued that I am still
relying on my GUI. Actually, I just like the way emacs looks from x. I
am doing everything else from the command line in the term, mainly
because of how simple it makes life. I mean I wouldn't have believed
it before, but you know, a GUI can really slow you down.

Anyway, when I left microsoft behind for good, I discovered that
mandrake had set up python as part of the default install (along with
alot of other really top-drawer development tools). I remembered the
show "call for help" that I used to watch on ZDTV (now techtv), a
program for windows users hosted (and this is a hoot) by a couple of
IT guys that just happen to both be mainly linux and mac users! 

Anyway, they had suggested python as a good first language, and I had
been looking for a good language to get my feet wet after a long
abscense from programming. So when I saw that it was in my default
Linux install, I got curious again. I am a rank newbie. I mean, a real
baby in my python education. I have a good understanding of basic
programming concepts, which makes things easier, but I am learning to
potty train when it comes to learning python.

Having said that, I asked for a reference in this group for some
information about NNTP protocols for experimenting in python.

Jim steered me to pythons standard NNTPLIB and suggested I not
reinvent the wheel. Boy was he right. With Python, it was so easy to
access news servers "raw" that I had to pick up my jaw off the
floor. Jeeze! In 45 minutes, I whipped up a program that I *could*
*not* have done in the $100.00 visual basic 6.0 learning edition that I
had, because VB learning edition intentionally lacks the sockets
libraries (or it did when I bought it). That's so you will buy the VB
developers pack for what, $400.00? $500.00?

What do I have to say about VB? "It's dead Jim".

This is the code I whipped up. It opens a connection to the server,
and gets the ident and stats. It then prints the last 10 available
messages before closing the connection. No big deal. But for this
newbie, a very big deal.


#####################################
# load needed libraries and modules #
#####################################

# load module required for interfacing to usenet servers
# via NNTP. Documentation for this library is at:
# http://www.python.org/doc/current/lib/module-nntplib.html
from nntplib import *

# Load sys module for system calls like argv[]
from sys import *


################################
# Declare persistent variables #
# (yes I know I don't have to) #
################################

# news server - replace with your own server
nntpserver  = "news.yourserver.com"

# newsgroup we are interested in
newsgroup = "comp.os.linux.advocacy"


#################
# Begin program #
#################

# Open connection to server
s = NNTP(nntpserver)

# Ask the sever for it's welcome message (usually just a system ID)
# if one is available. Print the message. Min. response is a three digit code
welcome = s.getwelcome()
print "\n\nOpening connection to", nntpserver
print "querying server for login message ....."

if len (welcome) < 3:
    print "\nServer has no response to query. Don't worry, it happens."
else:
    print "\nServer says: ", welcome

# Retrieve and print information on the newsgroup we are interested in
print "\n\n"
resp, count, first, last, name = s.group(newsgroup)
print "Group", name, "has", count, "articles, range", first, "to", last
print

# Retrieve and print subject information
resp, subs = s.xhdr("subject", first+"-"+last)
for id, sub in subs[-10:]: print id,  sub

# close the connection
s.quit()


###############
# End program #
###############

Some of this is borrowed from the NNTPLIB example page.

Elegant, isn't it? Python is like that. So simple, yet so
powerful. And alot of the work has been done for you already. Like the
NNTPLIB module. or the SMTPLIB module. They make accessing servers
with these protocols painless.

Python is what BASIC should have been. Easy for beginners, powerful
for advanced applications, able to seamlessly integrate with C, object
oriented. I *love* this language. 

I said that it seems to just work better in Linux. I should have said,
there are snags in windows that have to do with the way windows
handles things. For instance, the line:

from nntplib import * 

should make all of nntplib's methods available to the calling
program. However, a known problem (for which there is a workaround -
as is the way with windows) is that due to windows file system quirks,
the libraries are not always in the same place, or with the same
names. for instance, according to the docs, windows often will
capitalize an 8.3 filename all on it's own, or not treat spaces
correctly. The trolls will jump on this, but I trust the python docs
more than I trust the word of trolls. And it is in line with my
experiences. 

Stuff just works better in Linux. What else can you say.

Oh, I know what else ..... PYTHON ROCKS!

Cheers,

Mathew

XPosted: comp.os.linux.advocacy, comp.lang.python

-- 
TuxTrax   (n.) An invincible, all terrain, Linux driven armored assault
vehicle that can take as much fire as it gives ;-)

Yes, I am a Penguin cult high priest. Flipper readings upon request.

ROT13 this email address to mail me: uvtuqrfregzna at lnubb.pbz




More information about the Python-list mailing list