The urllib Module

A high-level interface to HTTP and FTP

  • Provides a file-like object that can be used to connect to remote servers
     import urllib
     f = urllib.urlopen("http://www.python.org/index.html")
     data = f.read()
     f.close() 

Utility functions

     urllib.quote(str)        # Quotes a string for use in a URL
     urllib.quote_plus(str)   # Also replaces spaces with '+'
     urllib.unquote(str)      # Opposite of quote()
     urllib.unquote_plus(str) # Opposite of quote_plus()
     urllib.urlencode(dict)   # Turns a dictionary of key=value
                              # pairs into a HTTP query-string 
  • Examples
     urllib.quote("beazley@cs")      # Produces "beazley%40cs"
     urllib.unquote("%23%21/bin/sh") # Produces "/bin/sh"
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 95
July 17, 2000, beazley@cs.uchicago.edu
>>>