http post and get the parameter from url

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SaraswathiIndirarajan
    New Member
    • Jan 2009
    • 3

    http post and get the parameter from url

    Hi,

    I have a script client.py as:
    Code:
    import httplib
    
    http = httplib.HTTP("localhost:8081")
    data="Welcome to My Page"
    http.putrequest("POST", "/cgi-bin/server.py?id="+data)
    http.putheader("Host", "localhost:8081")
    http.putheader("Content-Type", 'text/plain')
    http.putheader("Content-Length", str(len(data)))
    http.endheaders()
    http.send(data)
    Server.py:
    Code:
    import httplib
    import cgi
    
    print "Content-Type:text/plain"
    print
    
    id=cgi.FieldStorage()
    print id
    ------------------------------

    I got the malformed header from script. Bad header=200: error.

    I want to read the posted data from Client.py in Server.py. How do i get the parameter value from the url. Kindly help me.
    Last edited by bvdet; Feb 23 '09, 05:22 PM. Reason: Added code tags
  • sharat87
    New Member
    • Feb 2009
    • 6

    #2
    hello,

    i'm not sure it would work... but try this

    instead of the following two lines in your client.py,
    Code:
    data="Welcome to My Page"
    http.putrequest("POST", "/cgi-bin/server.py?id="+data)
    do this...

    Code:
    import urllib
    data = "Welcome to my page"
    http.putrequest("POST", "/cgi-bin/server.py?id=" + urllib.quote(data))
    what this does is that it will escape the characters like the (space) in your data in a protocol friendly manner. to undo this escaping, you can use the
    Code:
    urllib.unquote(s)
    function

    if this does not work.. take a look at the urllib.urlencod e function.. that might help ya!

    regards,
    sharat87

    Comment

    Working...