import urllib
import urllib2
# DATA:
# option #1 - using a dictionary
values = {'name': 'Michael Foord', 'location': 'Northampton', 'language': 'Python' }
data = urllib.urlencode(values)
# option #2 - directly as a string
data = 'name=Michael+Foord&language=Python&location=Northampton'
# HEADERS:
# option #1 - convert a bulk of headers to a dictionary (really, don't do this)
headers = '''
Host: www.http.header.free.fr
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
Accept-Language: Fr
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)
Connection: Keep-Alive
'''
headers = dict([[field.strip() for field in pair.split(':', 1)] for pair in headers.strip().split('\n')])
# option #2 - just use a dictionary
headers = {'Accept': 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'Fr',
'Connection': 'Keep-Alive',
'Host': 'www.http.header.free.fr',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'}
# send the request and receive the response
req = urllib2.Request('http://www.someserver.com/cgi-bin/register.cgi', data, headers)
response = urllib2.urlopen(req)
the_page = response.read()