#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    5
    Rep Power
    0

    BaseHTTPServer and cgi.FieldStorage


    How come that I "cant't" use cgi.FieldStorage when using BaseHTTPServer that act as a server.

    I have put python script (small one) online at: http://polartech.no/gallery/oblig2.py

    As written in the source code as comments, the do_POST method is called, but cgi.FieldStorage() return an empty fieldstorage. The big question is, how can I get the form data? cgi.FieldStorage() doesn't help me here.

    Espen
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    BaseHTTPServer does not handle CGI requests. Try using CGIHTTPServer instead.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    5
    Rep Power
    0
    I have replaced

    class Handler(BaseHTTPServer.BaseHTTPRequestHandler):

    with this one

    class Handler(CGIHTTPServer.CGIHTTPRequestHandler):

    The replaced class header is located at:
    http://polartech.no/gallery/oblig3.py

    and it is still the same. Don't know what more to do, have searched through google thousands times now.

    Any1 with a solution?

    Espen
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    OK, I have looked at your code, and I see what the problem is.

    You can't embed the CGI code in the server code like that. The whole point of CGIHTTPServer (and CGI in general) is that it executes separate scripts in designated directories and returns the output. Because you had overridden the do_POST method, the code to parse the form data was never called.

    Use the CGIHTTPRequestHandler unmodified, and put the CGI code as a separate script in a subdirectory called cgi-bin.

    Dave - The Developers' Coach
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    5
    Rep Power
    0
    If I let the server redirect the post call to a cgi script in /cgi-bin/ how can I get the Base server to reach the form data that the cgi script had pulled from the form?

    Espen
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    1
    Rep Power
    0
    to do what you want use BaseHTTPServer
    and get the form data with

    form = cgi.parse_qs(self.rfile.read(int(self.headers.getheader('Content-Length'))))
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    5
    Rep Power
    0
    How do I parse this form? Is there some build in module that can parse such? I haven't come across anything.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'm assuming we're talking about the string returned when manually parsing posted form data - in which case i've gotta tell you it isn't easy!

    If you check out my NET module, in params.py you'll find a full featured form parser including posted data and file uploading. Everything is highly commented so you should be able to follow the steps pretty easily.

    Hope this helps,

    Have fun!

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

  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    5
    Rep Power
    0
    And where can I find it?

    Espen
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Sorry, i forgot to post the link, my bad . You can find the latest version of NET here:

    http://forums.devshed.com/t129666/s.html

    I havn't had much time to work on it lattly so not much has changed since then.

    Hope you find what your looking for.

    Netytan
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo