469,600 Members | 2,367 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Sending part of a page as the body of an email

Kun
I currently have a python-cgi script that extracts data from a mysql
table. I would like to save this data as a string and send it to a
recipient via email.

I know how to send email using the smtp lib, but what I do not know his
how to save a portion of the page as a string. Any pointers?
Apr 16 '06 #1
1 968
Kun wrote:
I currently have a python-cgi script that extracts data from a mysql
table. I would like to save this data as a string and send it to a
recipient via email.

I know how to send email using the smtp lib, but what I do not know his
how to save a portion of the page as a string. Any pointers?


#At the top of your CGI script import these
import sys
import StringIO

#Create a StringIO object to hold output
string_io = StringIO.StringIO()
#Save the old sys.stdout so we can go back to it later
old_stdout = sys.stdout

#Normal output goes here

#To start capturing output replace sys.stdout with the StringIO object
sys.stdout = string_io

#Captured output goes here

#To finish capturing
sys.stdout = old_stdout

#To get the captured text from the StringIO object do:
captured_text = string_io.getvalue()

#To output the captured text to the original sys.stdout
#If this is omitted, the captured_text will not be sent to the browser
old_stdout.write(captured_text)

#You can repeat this capturing section several times and the output
#will accumulate in the StringIO object. To clear the contents of the
#StringIO object between sections do:
#string_io.truncate(0)

#More normal output goes here
#Do whatever you want with captured_text here...
#When finished with string_io, close it to free up the memory buffers
string_io.close()
--
Jesse Hager
email = "wr********@tznvy.pbz".decode("rot13")
Apr 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by festivalman | last post: by
6 posts views Thread by Eduardo Rosa | last post: by
4 posts views Thread by splicemix | last post: by
6 posts views Thread by Anuradha | last post: by
4 posts views Thread by Roger Withnell | last post: by
2 posts views Thread by =?Utf-8?B?QWRl?= | last post: by
8 posts views Thread by Marty | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.