[Tutor] further ignorant babbling, but with prettier words

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 9 Dec 2001 14:44:16 -0800 (PST)


On Sun, 9 Dec 2001, Kirk Bailey wrote:

> Lookee what I been up to.

Cool!  Let's take a look.



> import sys, re, string, rfc822, smtplib
> 
> localhost = 'howlermonkey.net'	# make sure you set this to the domain YOU use!!!
> 
> pathtostuff = '/www/www.howlermonkey.net/cgi-bin/'	#please edit this to suit your system.
> 						# note this tells where to start looking.
> 						# everything is either here, ur under this
> 						# point in a systematic manner.
> 					
> listname = argv[1]			# we read a command line arguement to determine the list 


You'll probably need to use 'sys.argv' instead, because:

> import sys

doesn't automatically pull the 'argv' arguments variable out of the
module.  If we do something like:

###
argv = sys.argv
###

right at the beginning, though, that'll allow us to get at sys.argv by
just saying 'argv'.





> message = raw_input.lines()		# Variable 'message' is assigned the entire message

Try:

    message = sys.stdin.read()

instead.  raw_input() is a function that only knows how to read a single
line --- it won't respond favorably if we try to use it to read multiple
lines at a time:

###
>>> raw_input.lines()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: lines
###


'sys.stdin', on the other hand, is a "file":

    http://www.python.org/doc/lib/bltin-file-objects.html

that should know how to read():

###
>>> message = sys.stdin.read()
hello world
this is a test
I'd better stop now.
## At this point, I press Ctrl-D.  On Windows systems, Ctrl-Z should
## also get Python to stop reading from standard input.
>>> message
"hello world\nthis is a test\nI'd better stop now.\n"
###





> from = message["From"]

Before doing this, we'll probably need to 'parse' or process the message
string out to grab at the message's headers, probably using the rfc822
module we talked about a few days ago:

    http://www.python.org/doc/lib/module-rfc822.html



> open a.file(pathtostuff + "/lists/" + listname, 'r')

Try:

    a = open(pathtostuff + "/lists/" + listname)

which translates to "Let's assign 'a' to the open()ing of this file.  
What you had below tries to say "Try calling the file() method of 'a'"
which dosen't work, because Python doesn't know what 'a' means yet.



See if it works a little better.  Also, remember that you can always using
the interpreter to play around and experiment with Python.