http

Sample

PyMOTW: BaseHTTPServer

A simple HTTP Server: simple_http_server.py:

cd ~/your/folder/
python -m SimpleHTTPServer 8080

Download

from urllib import urlretrieve
urllib.urlretrieve(image_url, file_name)

GET

Here is an example session that uses the GET method to retrieve a URL containing parameters:

import urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
print f.read()

Parse

For parsing a URL, see URL

POST

The following example uses the POST method instead:

import urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
print f.read()

To post specific content type e.g. for SOLR XML requests:

url = 'http://localhost:8080/solr/'
xml = '<delete><id>%s</id></delete>' % id

req = urllib2.Request(url, xml)
req.add_header("Content-Type", "text/xml")
f = urllib2.urlopen(req)
response = f.read()
f.close()

Quote/Unquote

See html and xml, Quote/Unquote

Exception

try:
    return urllib2.urlopen(url, urllib.urlencode(params))
except urllib2.HTTPError, e:
    self.display(e, True)
    raise