Thread: CGI troubles

    #1
  1. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20

    CGI troubles


    I've recently started playing with CGI in Python, and since first couple of my CGI scripts worked fine, I thought I'd write an email form CGI script.
    But whenever I try to run it, the server issues premature end of script headers error, and I can't figure out what's wrong.


    Code:
    #!/usr/bin/python
     
    import smtplib
    import cgi
     
    def main():
    print "Content-type: text/html\n"
    form = cgi.FieldStorage()
    if form.has_key("name") and form["name"].value != "" and form.has_key("email") and form["email"].value != "":
    	 from = form["email"].value
    	 to = myemail@example.com
    	 message = form["comment"].value
     
    server = smtplib.SMTP('localhost')
    server.set_debuglevel(1)
    server.sendmail(from, to, message)
    server.quit()
     
     
     
    print "<html><head><title>Sent</title></head>"
    print "<body bgcolor = #ffcc66><h1 align=center>",
    print "<p>Your message has been sent!</body>"
    print "</html>"
     
    main()
    
    My form.html:

    Code:
    <html>
    <head>
    <title>Email Form</title>
    </head>
     
    <body>
    <FORM METHOD="POST" ACTION="sendMail.cgi">
    <INPUT TYPE=HIDDEN NAME="key" VALUE="process">
    Name:<BR>
    <INPUT TYPE=TEXT NAME="name" size=50>
    <BR>
    Email: 
    <BR>
    <INPUT TYPE=TEXT NAME="email" size=50>
    <BR>
    <P>
    Comments:<BR>
    <TEXTAREA NAME="comment" ROWS=8 COLS=60>
    </TEXTAREA>
    <P> 
    <INPUT TYPE="SUBMIT" VALUE="Send">
    </FORM>
     
    </body>
    </html>
    
    The server requires files to end in .cgi, but again, my other .cgi scripts written in Python worked just fine.

    So what am I not seeing here?
    Any help would be appreciated.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    It is probably that you've used the reserved/keyword from as a variable.

    Also it looks like your code has a mix of tabs and spaces.

    BTW, I debugged your code by pasting it into IDLE and selecting Alt-X Check code in the Run menu.

    grimey
    Last edited by Grim Archon; November 14th, 2004 at 03:06 AM.
  4. #3
  5. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    OK, I renamed my from variable to fromaddress, and I used spaces only for indentation, but it still get the same error. Any ideas?
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    You could try switching on the cgi traceback
    Code:
    import cgitb
    cgitb.enable()
    grim
  8. #5
  9. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    Originally Posted by Grim Archon
    You could try switching on the cgi traceback
    Code:
    import cgitb
    cgitb.enable()
    grim
    Ok, so now this is what I have:

    Code:
     
    #!/usr/bin/python
     
    import cgitb
    cgitb.enable()
    import cgi
    import smtplib
     
     
    def main():
    print "Content-type: text/html\n"
    form = cgi.FieldStorage()
    if form.has_key("name") and form["name"].value != "":
    	fromaddress = form["email"].value
    	toaddress = my@email.com
    	message = form["comment"].value
     
    server = smtplib.SMTP('localhost')
    server.set_debuglevel(1)
    server.sendmail(fromaddress, toaddress, message)
    server.quit()
     
    print "<html><head><title>Sent</title></head>"
    print "<body bgcolor = #ffcc66><h1 align=center>",
    print "<p>Your message has been sent!</body>"
    print "</html>"
     
    main()
    
    But I get the same error message...
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Your not getting a Syntax/Indentation error are you. If this is the case then it has to do with the indentation/layout of your main() function. Give this a go:

    Code:
    #!/usr/bin/python
     
    import cgitb
    cgitb.enable()
    import cgi
    import smtplib
    
    print "Content-type: text/html\n"
     
    def main():
        form = cgi.FieldStorage()
        if form.has_key("name") and form["name"].value != "":
            fromaddress = form["email"].value
            toaddress = my@email.com
            message = form["comment"].value
     
        server = smtplib.SMTP('localhost')
        server.set_debuglevel(1)
        server.sendmail(fromaddress, toaddress, message)
        server.quit()
     
        print "<html><head><title>Sent</title></head>"
        print "<body bgcolor = #ffcc66><h1 align=center>",
        print "<p>Your message has been sent!</body>"
        print "</html>"
     
    if __name__ == '__main__': main()
    Note: I've made a few changes to your CGI script i.e. the header has been moved outside your function; this is just good practive (this way you cant accidentaly output something before your headers have been sent.)

    Also, Python doesn't require you to create a main() function so you might consider loosing it all together and moving the code into the if statement.

    Night,

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    I'm intrigued by this problem as well. i personally would have used the getvalue function
    Code:
    getvalue(value,"defaultvalue")
    which supplies a default value if none is given. but i suppose that is a preference. As to your code. I simply get the following cgitb msg when i run it locally
    C:\Program Files\Abyss Web Server\htdocs\test\sendMail.py
    23 print "<body bgcolor = #ffcc66><h1 align=center>",
    24 print "<p>Your message has been sent!</body>"
    25 print "</html>"
    26
    27 if __name__ == '__main__': main()
    __name__ = '__main__', main = <function main at 0x008E0030>

    C:\Program Files\Abyss Web Server\htdocs\test\sendMail.py in main()
    15 message = form["comment"].value
    16
    17 server = smtplib.SMTP('localhost')
    18 server.set_debuglevel(1)
    19 server.sendmail(fromaddress, toaddress, message)
    server undefined, global smtplib = <module 'smtplib' from 'C:\Python23\lib\smtplib.pyc'>, smtplib.SMTP = <class smtplib.SMTP at 0x009B4150>

    C:\Python23\lib\smtplib.py in __init__(self=<smtplib.SMTP instance at 0x009744B8>, host='localhost', port=0, local_hostname=None)
    238 self.esmtp_features = {}
    239 if host:
    240 (code, msg) = self.connect(host, port)
    241 if code != 220:
    242 raise SMTPConnectError(code, msg)
    code undefined, msg undefined, self = <smtplib.SMTP instance at 0x009744B8>, self.connect = <bound method SMTP.connect of <smtplib.SMTP instance at 0x009744B8>>, host = 'localhost', port = 0

    C:\Python23\lib\smtplib.py in connect(self=<smtplib.SMTP instance at 0x009744B8>, host='localhost', port=25)
    300 break
    301 if not self.sock:
    302 raise socket.error, msg
    303 (code, msg) = self.getreply()
    304 if self.debuglevel > 0: print "connect:", msg
    global socket = <module 'socket' from 'C:\Python23\lib\socket.pyc'>, socket.error = <class socket.error at 0x008EE5A0>, msg = <socket.error instance at 0x009745A8>

    error: (10061, 'Connection refused')
    args = (10061, 'Connection refused')
    it seems to me as if the smtplib isn't even intialized and as a result no connection is made. I say this because i run my comp as a server as well and i write and send msgs to local host and files on my hd all the time.
    interested to know what the problem is.
    Also shouldn't the from addy be a string? I changed that as well.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  14. #8
  15. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    Oh, gees, the indentation was originally fine, it was when I was doing c/p to post the message that I forgot to format it again. I made some modifications and it seems to work.
    Tnx a lot for your help.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Originally Posted by Boki
    Oh, gees, the indentation was originally fine, it was when I was doing c/p to post the message that I forgot to format it again. I made some modifications and it seems to work.
    Tnx a lot for your help.
    What were the modifications. Please share, so if anyone has a similiar problem....
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  18. #10
  19. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    Well, I managed to make it work in the command line, and for now I am happy with that... When I find some time I'll debug my web implementation of it, too. I'm prettyo sure it's something so obvious, but at the moment, I just can't see it.

IMN logo majestic logo threadwatch logo seochat tools logo