POSTing a form using Python and Curl
Question: I am relatively new (as in a few days) to Python - I am looking for an example
that would show me how to post a form to a website (say www.example.com).

I already know how to use Curl. Infact, I have written C+++ code that does
exactly the same thing (i.e. POST a form using Curl), but I would like some
starting point (a few lines from which I can build on), which will show me how
to do this using Python.


Answer: Here is an example using urllib and urllib2 for both POST and GET:

**POST** \- If `urlopen()` has a second parameter then it is a POST request.

    
    
    import urllib
    import urllib2
    
    url = 'http://www.example.com'
    values = {'var' : 500}
    
    data = urllib.urlencode(values)
    response = urllib2.urlopen(url, data)
    page = response.read()
    

**GET** \- If `urlopen()` has a single parameter then it is a GET request.

    
    
    import urllib
    import urllib2
    
    url = 'http://www.example.com'
    values = {'var' : 500}
    
    data = urllib.urlencode(values)
    fullurl = url + '?' + data
    response = urllib2.urlopen(fullurl)
    page = response.read()
    

You could also use `curl` if you call it using `os.system()`.

Here are some helpful links:  
<http://docs.python.org/library/urllib2.html#urllib2.urlopen>  
<http://docs.python.org/library/os.html#os.system>

