(slightly OT): Python and linux - very cool

RPM1 rpm1deletethis at nospamfrontiernet.net
Thu Aug 1 07:42:54 EDT 2002


"TuxTrax" <TuxTrax at fortress.tuxnet.net> wrote in message
news:slrnakhuhq.la6.TuxTrax at fortress.tuxnet...

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

Interesting, I just did the same thing, (on Windows).  I have some extra
functionality,
'!'    -    Prompt for group and get last 10 posts from the group.
'+'    -    Get next 10 posts.
'-'    -    Get previous 10 posts
'*'    -    Reprint the current list of posts
0..9    -    Show post #x of the current list
'quit'    -    To quit

Here's the code I whipped up, (in about 90 minutes).  It's kind of ugly but
it works:

######## Start ############
from nntplib import *


def displayPosts(s,start,last):
    resp, subs = s.xhdr('subject', start + '-' + last)
    subs.reverse()
    count = 0
    for id, sub in subs:
        header = s.head(id)
        dateWhen = ''
        fromWho = ''
        subjectWhat = ''
        for x in header[3]:
            if x.find('From:') != -1:
                fromWho = x
            if x.find('Subject:') != -1:
                subjectWhat = x
            if x.find('Date:') != -1:
                dateWhen = x

        dateStart = dateWhen.find(':')
        if dateStart < 0:
            print count, sub
        else:
            print count, sub, dateWhen[dateStart:]
        count += 1

    return subs





s = NNTP('news.readfreenews.net')

stepSize = 9

finished = 0
g = 'None'
newsPrompt = 'news> '
while not(finished):
    response = raw_input(newsPrompt)
    if response.find('quit') != -1:
        break

    if response.find("+") != -1:
        if (int(last) + stepSize) > int(groupLast):
            start = str(int(groupLast) - stepSize)
            last = groupLast
            print 'End of articles'
        else:
            start = str(int(start) + stepSize)
            last = str(int(last) + stepSize)
        subs = displayPosts(s,start,last)

    if response.find("-") != -1:
        if (int(start) - stepSize) < int(groupFirst):
            print 'Beginning of articles'
            start = str(int(groupFirst))
            last = str(int(start) + stepSize)
        else:
            start = str(int(start) - stepSize)
            last = str(int(last) - stepSize)
        subs = displayPosts(s,start,last)

    if response.find('!') != -1:
        g = raw_input('Group? : ')
        print
        print g
        print
        try:
            resp, count, groupFirst, groupLast, name = s.group(g)
        except:
            continue
        print 'Group', name, 'has', count, 'articles, range', groupFirst,
'to', groupLast

        newsPrompt = name + '> '
        start = str(int(groupLast) - stepSize)
        last = groupLast
        subs = displayPosts(s,start,last)

    if response.isdigit():
        if g != 'None':
            articleNumber = int(response)
            if articleNumber > 9 or articleNumber < 0:
                continue
            id, sub = subs[articleNumber]
            bodyInfo = s.body(id)
            header = s.head(id)
            print
            print 'Header =============================='
            print
            for x in header[3]:
                if x.find('From:') != -1:
                    print x
                if x.find('Subject:') != -1:
                    print x
                if x.find('Date:') != -1:
                    print x
            print '====================================='
            print
            for l in bodyInfo[3]:
                print l
        else:
            print 'No newsgroup loaded'

    if response.find('*') != -1:
        subs = displayPosts(s,start,last)


s.quit()
######## End ############

Patrick






More information about the Python-list mailing list