Problem with running python cgi scripts through my browser

kreta06 kreta06 at gmail.com
Fri Apr 20 09:21:01 EDT 2012


Dear Python Tutors,

I am having problems trying to send an email (using the smtplib module ) to
send an email to myself over my browser using a python script, which is
being called by an html form that I have created. My python cgi script is
located in my cgi-bin directory, which I had made sure that in my httpd
configuration, I had added the python extensions so that it can be run as a
cgi script over my browser. If I run my python script directly on the
commandline, I am able to send an email to myself and receive it.
However, when I try to do the same and run it from my html form, it doesn't
work. I'm sure I'm missing something here, but just can't figure out what.
My suspicions could be that to send an email over the browser requires a
specific format, which I could not figure out.
Below is my cgi script and errors that I get when I run the cgi script.

Any help would be appreciated.

Many thanks,
Sue


-----------------------------------------------------------------------------
my script:

import cgi
import string,sys
import smtplib
import cgitb
cgitb.enable()
sys.stderr = sys.stdout


def main():
        print "Content-type: text/html\n"
        form = cgi.FieldStorage()
        if form.has_key("firstname") and form["firstname"].value != "":
                result = "<p>Name: " + form["firstname"].value +
"<p>Favorite Color(s): " + form["colors"].value + "<p>Favorite Drinks(s): "
+ str(form.getlist("drinks")) + "<p>Email: " + form["email"].value
                return result
        else:
                print "Please enter your firstname!"


TEXT = main()


FROM = "myemail at gmail.com"
TO = "myemail at gmail.com"
SUBJECT = "Second try"

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM,",".join(TO), SUBJECT, TEXT)

# Send the mail
server2 = smtplib.SMTP("localhost")
server2.sendmail(FROM, TO, message)
server2.quit()

---------------------------------------------

Error  I get:

 /usr/lib/python2.6/cgitb.py:173: DeprecationWarning: BaseException.message
has been deprecated as of Python 2.6 value =
pydoc.html.repr(getattr(evalue, name))

*<class 'socket.error'>*Python 2.6.6: /usr/bin/python
Fri Apr 20 15:13:06 2012

A problem occurred in a Python script. Here is the sequence of function
calls leading up to the error, in the order they occurred.
  /var/www/cgi-bin/action.py in **()    37
    38 # Send the mail
    39 server2 = smtplib.SMTP("localhost")
    40 server2.sendmail(FROM, TO, message)
    41 server2.quit()
 server2 *undefined*, *smtplib* = <module 'smtplib' from
'/usr/lib/python2.6/smtplib.pyc'>, smtplib.*SMTP* = <class smtplib.SMTP>
/usr/lib/python2.6/smtplib.py in *__init__*(self=<smtplib.SMTP instance>,
host='localhost', port=0, local_hostname=None, timeout=<object object>)
  237         self.default_port = SMTP_PORT
   238         if host:
   239             (code, msg) = self.connect(host, port)
   240             if code != 220:
   241                 raise SMTPConnectError(code, msg)
 code *undefined*, msg *undefined*, *self* = <smtplib.SMTP instance>, self.*
connect* = <bound method SMTP.connect of <smtplib.SMTP instance>>, *host* =
'localhost', *port* = 0   /usr/lib/python2.6/smtplib.py in
*connect*(self=<smtplib.SMTP
instance>, host='localhost', port=25)   293
         if not port: port = self.default_port
   294
         if self.debuglevel > 0: print>>stderr, 'connect:', (host, port)
   295         self.sock = self._get_socket(host, port, self.timeout)
   296         (code, msg) = self.getreply()
   297         if self.debuglevel > 0: print>>stderr, "connect:", msg
 *self* = <smtplib.SMTP instance>, self.sock *undefined*, self.*_get_socket* =
<bound method SMTP._get_socket of <smtplib.SMTP instance>>, *host* =
'localhost', *port* = 25, self.*timeout* = <object object>
/usr/lib/python2.6/smtplib.py in *_get_socket*(self=<smtplib.SMTP
instance>, port='localhost', host=25, timeout=<object object>)   271
         # and just alter the socket connection bit.
   272
         if self.debuglevel > 0: print>>stderr, 'connect:', (host, port)
   273         return socket.create_connection((port, host), timeout)
   274
   275     def connect(self, host='localhost', port = 0):
 *global* *socket* = <module 'socket' from
'/usr/lib/python2.6/socket.pyc'>, socket.*create_connection* = <function
create_connection>, *port* = 'localhost', *host* = 25, *timeout* = <object
object>   /usr/lib/python2.6/socket.py in
*create_connection*(address=('localhost',
25), timeout=<object object>)   563         except error, msg:
   564             if sock is not None:
   565                 sock.close()
   566
   567     raise error, msg
 *global* *error* = <class 'socket.error'>, *msg* = error(13, 'Permission
denied')

*<class 'socket.error'>*: [Errno 13] Permission denied
      args = (13, 'Permission denied')
      errno = 13
      filename = None
      message = ''
      strerror = 'Permission denied'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120420/fe330d51/attachment.html>


More information about the Python-list mailing list