Receive HTTP POST response by python
Question: I use the following example: <http://www.w3schools.com/php/php_forms.asp>

When I run it from browser, I see the results in the browser:

    
    
    Welcome John
    Your email address is john.doe@example.com
    

When I run python POST http request:

    
    
    import httplib, urllib
    params = urllib.urlencode({'@name': 'John','@email': 'John.doe@example.com'})
    headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/html"}
    conn = httplib.HTTPConnection("10.0.0.201")
    conn.request("POST","/welcome.php",params, headers)
    response = conn.getresponse()
    print "Status"
    print response.status
    print "Reason"
    print response.reason
    print "Read"
    print response.read()
    conn.close()
    

I see the following:

    
    
    Status
    200
    Reason
    OK
    Read
    <html>
    <body>
    
    Welcome <br>
    Your email address is: 
    </body>
    </html>
    

The question is: How to receive POST request data in python?


Answer: You are using the wrong form names _and_ the wrong HTTP method. There are no
`@` characters at their starts:

    
    
    params = urllib.urlencode({'name': 'John','email': 'John.doe@example.com'})
    

Next, the form you point to uses _GET_ , not _POST_ as the handling method, so
you'll have to add these parameters to the URL instead:

    
    
    conn.request("GET", "/welcome.php?" + params, '', headers)
    

You are doing yourself a disservice by trying to drive the `HTTPConnection()`
manually. You could use
[`urllib2.urlopen()`](https://docs.python.org/2/library/urllib2.html#urllib2.urlopen)
instead for example:

    
    
    from urllib2 import urlopen
    from urllib import urlencode
    
    params = urlencode({'name': 'John','email': 'John.doe@example.com'})
    response = urlopen('http://10.0.0.201/welcome.php?' + params)
    print response.read()
    

or you could make use of the [`requests` library](http://docs.python-
requests.org/en/latest/) (separate install) to make it yourself much easier
still:

    
    
    import requests
    
    params = {'name': 'John','email': 'John.doe@example.com'}
    response = requests.get('http://10.0.0.201/welcome.php', params=params)
    print response.content
    

