parse GET/POST data on simple http server

Discussion in 'Python' started by Markus, Feb 3, 2011.

  1. Markus

    Markus Guest

    Hi,

    As a beginner in python, I am looking for example code that would help
    me understand how to
    code following idea:
    1. Start minimal http server
    2. Send GET or POST data (url encoded, or from form) - example
    Name="Foo"
    3. Analyze the GET/POST variable value on server and match to
    different value
    example 'if Name = "Foo" then retval = "Bar" '
    4. serve the content of retval back to user as plain html

    If some code snipped that does implement all or part of the algorithm
    is known to you, please point me to it. I would be thankful for any
    push to the right direction.

    Thank you!
    Markus, Feb 3, 2011
    #1
    1. Advertising

  2. On 02/03/2011 03:15 AM, Markus wrote:
    > Hi,
    >
    > As a beginner in python, I am looking for example code that would help
    > me understand how to
    > code following idea:
    > 1. Start minimal http server
    > 2. Send GET or POST data (url encoded, or from form) - example
    > Name="Foo"
    > 3. Analyze the GET/POST variable value on server and match to
    > different value
    > example 'if Name = "Foo" then retval = "Bar" '
    > 4. serve the content of retval back to user as plain html
    >
    > If some code snipped that does implement all or part of the algorithm
    > is known to you, please point me to it. I would be thankful for any
    > push to the right direction.
    >
    > Thank you!


    If you really can't use a 'real' webserver like Apache, I found [1]. Not
    sure how to use it, never had the need to. The documentation will show
    you the way, however. Using that and the cgi module, your requirements
    should be fulfilled.
    Corey Richardson, Feb 3, 2011
    #2
    1. Advertising

  3. On 02/03/2011 03:15 AM, Markus wrote:
    > Hi,
    >
    > As a beginner in python, I am looking for example code that would help
    > me understand how to
    > code following idea:
    > 1. Start minimal http server
    > 2. Send GET or POST data (url encoded, or from form) - example
    > Name="Foo"
    > 3. Analyze the GET/POST variable value on server and match to
    > different value
    > example 'if Name = "Foo" then retval = "Bar" '
    > 4. serve the content of retval back to user as plain html
    >
    > If some code snipped that does implement all or part of the algorithm
    > is known to you, please point me to it. I would be thankful for any
    > push to the right direction.
    >
    > Thank you!


    [1] http://docs.python.org/library/cgihttpserver.html#module-CGIHTTPServer

    ....Sorry about that. I shouldn't post late at night!
    Corey Richardson, Feb 3, 2011
    #3
  4. Markus

    Chris Rebert Guest

    On Thu, Feb 3, 2011 at 12:15 AM, Markus <> wrote:
    > Hi,
    >
    > As a beginner in python, I am looking for example code that would help
    > me understand how to
    > code following idea:
    > 1. Start minimal http server


    http://docs.python.org/library/basehttpserver.html
    http://docs.python.org/library/simplehttpserver.html
    http://docs.python.org/library/cgihttpserver.html

    > 2. Send GET or POST data (url encoded, or from form) - example
    > Name="Foo"


    http://docs.python.org/library/urllib.html#urllib.urlencode

    > 3. Analyze the GET/POST variable value on server and match to
    > different value
    >    example 'if Name = "Foo" then retval = "Bar" '


    http://docs.python.org/library/cgi.html

    > 4. serve the content of retval back to user as plain html
    >
    > If some code snipped that does implement all or part of the algorithm
    > is known to you, please point me to it. I would be thankful for any
    > push to the right direction.


    You'll be reinventing quite a few wheels if you work at such a low
    level of abstraction. Have you considered using a web framework?
    Django (http://www.djangoproject.com/ ) is one of the popular ones,
    though there are a myriad of options
    (http://wiki.python.org/moin/WebFrameworks ). I would recommend
    learning Python first and then a web framework, rather than trying to
    learn both in tandem.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Feb 3, 2011
    #4
  5. Markus

    Markus Guest

    On Feb 3, 9:35 am, Chris Rebert <> wrote:
    > On Thu, Feb 3, 2011 at 12:15 AM, Markus <> wrote:
    > > Hi,

    >
    > > As a beginner in python, I am looking for example code that would help
    > > me understand how to
    > > code following idea:
    > > 1. Start minimal http server

    >
    > http://docs.python.org/library/base...://docs.python.org/library/cgihttpserver.html
    >
    > > 2. Send GET or POST data (url encoded, or from form) - example
    > > Name="Foo"

    >
    > http://docs.python.org/library/urllib.html#urllib.urlencode
    >
    > > 3. Analyze the GET/POST variable value on server and match to
    > > different value
    > >    example 'if Name = "Foo" then retval = "Bar" '

    >
    > http://docs.python.org/library/cgi.html
    >
    > > 4. serve the content of retval back to user as plain html

    >
    > > If some code snipped that does implement all or part of the algorithm
    > > is known to you, please point me to it. I would be thankful for any
    > > push to the right direction.

    >
    > You'll be reinventing quite a few wheels if you work at such a low
    > level of abstraction. Have you considered using a web framework?
    > Django (http://www.djangoproject.com/) is one of the popular ones,
    > though there are a myriad of options
    > (http://wiki.python.org/moin/WebFrameworks). I would recommend
    > learning Python first and then a web framework, rather than trying to
    > learn both in tandem.
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com


    Thank you for all that input, I will definitely check Django - it
    looks very interesting.
    I just found an example code that fits perfectly and is simple enough
    for me to play with it.
    http://stackoverflow.com/questions/336866/how-to-implement-a-minimal-server-for-ajax-in-python
    And one older post handling the same case with HTTPS:
    http://groups.google.com/group/comp...?lnk=gst&q=server parse post#2ff6f704b3a6749a

    :eek:)
    Markus, Feb 3, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vm
    Replies:
    2
    Views:
    456
  2. rup
    Replies:
    10
    Views:
    707
    CBFalconer
    Jan 7, 2007
  3. serge calderara

    HTTP GET vs HTTP POST protocol

    serge calderara, Nov 4, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    585
  4. n3d!m

    Http post and http get

    n3d!m, Jan 25, 2012, in forum: Python
    Replies:
    2
    Views:
    350
    n3d!m
    Feb 6, 2012
  5. Fred
    Replies:
    17
    Views:
    248
Loading...

Share This Page