REPOST: Re: (Almost) command line simulation with execfile

Stefan Schwarzer s.schwarzer at ndh.net
Sun Dec 30 21:02:09 EST 2001


Hello Brad

Brad Clements wrote:
> Please post internet.py

Here it is:

'''
Some internet-related modules
'''

import socket
import urllib
import smtplib
import time

def url_content_part(url, count=None):
    '''Return a number of bytes from an URL.'''
    fetched = urllib.urlopen(url)
    if count is None:
        text = fetched.read()
    else:
        text = fetched.read(count)
    fetched.close()
    return text

def send_example_email(server, from_addr, to_addr):
    '''Send an email (with fixed text).'''
    date = time.strftime( '%a, %d %b %Y %H:%M:%S', time.localtime() )
    text = '''\
From: %(from_addr)s
To: %(to_addr)s
Date: %(date)s
Subject: A little Python example for sending mails

Hello user

This is a mail from %(from_addr)s to %(to_addr)s
via server %(server)s

Best regards
Your Python interpreter ;-)\n''' % vars()
    host = smtplib.SMTP(server)
    host.sendmail(from_addr, [to_addr], text)
    host.quit()


raw_input(
  'Please ensure you are connected to the internet and press [Return]: ')

print

try:
    url = 'http://www.python.org/'
    count = 150
    print 'The first %s characters from %s:' % (count, url)
    print url_content_part(url, count)

    print

    print 'Sending an email:'
    server = raw_input('Enter a valid mailserver (full name) for your domain: ')
    from_addr = raw_input('Enter FROM email address: ')
    to_addr = raw_input('Enter TO email address: ')
    send_example_email(server, from_addr, to_addr)
    print 'Mail has been sent.'
except (IOError, socket.error):
    print
    print 'There seems to be a problem with your internet connection.'
    print 'Are you really online? Is all set up?'
except smtplib.SMTPException:
    print
    print 'Sending your mail failed. Please check your parameters and'
    print 're-execute the program with the correct ones.'

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-nwblwi.newsops.execpc.com!newsfeeds.sol.net!news-out.visi.com!hermes.visi.com!newsxfer.interpacket.net!news-xfer.nuri.net!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: Stefan Schwarzer <s.schwarzer at ndh.net>
Newsgroups: comp.lang.python
Subject: cmsg cancel <3C2FC721.145D2EBC at ndh.net>
Control: cancel <3C2FC721.145D2EBC at ndh.net>
Date: Mon, 31 Dec 2001 03:13:55 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.3C2FC721.145D2EBC at ndh.net>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009773967 27193 211.57.49.2 (31 Dec 2001 04:46:07 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 04:46:07 +0000 (UTC)
X-No-Archive: yes
X-Unac4ncel: yes
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)

This message was cancelled from within Mozilla.



More information about the Python-list mailing list