Jeff Hammerbacher
Jeff Hammerbacher

Reputation: 4246

Does the Python standard library contain a module for manipulating URIs?

I'd like to pass a URI to a constructor and get back an object on which I can call obj.type, obj.host, obj.port, etc. The "Request" object of the urllib2 module is close to what I need, but not quite it.

Upvotes: 1

Views: 142

Answers (1)

gimel
gimel

Reputation: 86432

Maybe something like the urlparse module?

The urlparse module is renamed to urllib.parse in Python 3.0.

From the doc:

>>> from urlparse import urlparse
>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
>>> o   # doctest: +NORMALIZE_WHITESPACE
    ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
    params='', query='', fragment='')
>>> o.scheme
'http'
>>> o.port
80
>>> o.geturl()
'http://www.cwi.nl:80/%7Eguido/Python.html'

Upvotes: 6

Related Questions