sumid
sumid

Reputation: 2063

Is there a predefined class for URL in Python?

I am looking for something like java.net.URL in python-modules, Django, Zope or wherever in Python. I want it preferably from the semantics reason, because the result of analysis of concerned program implies that the URL plays an essential role in it. The consequence is that such URL class also will have great practical usage in that program.

Of course I could write such class on my own, but I'd like to look around before I start to reinvent the wheel.

I did look at urllib2 and urlparse. The urlparse basically has the functionality I need, but it doesn't encapsulate it into a class like java.net.URL. Regarding my analysis of my program it works upside-down.

I looked also into the source code of urlparse at the classes SplitResult and ParseResult. They have some basic functionality and they can be used for subclassing. But I'll have to rewrite rest of the urlparse functions as the subclass methods.

I found also mxURL - Flexible URL Datatype for Python. It is very close to what I really want. Only it seems to be quite an overkill for my purpose.

Can anyone suggest another option? Should I proceed with reinventing the wheel?

My solution:

To get my URL class I did basically two things:

  1. Inherit from urlparse.ResultMixin.
  2. Define function which only calls urlparse.urlparse() and transforms results to parameters of URL instance.

Upvotes: 39

Views: 26117

Answers (6)

user3076105
user3076105

Reputation: 416

>>> import urllib3
>>> help(urllib3.util.url)
...
CLASSES
Url(builtins.tuple)
    Url

class Url(Url)
 |  Url(scheme=None, auth=None, host=None, port=None, path=None, query=None, fragment=None)
 |  
 |  Data structure for representing an HTTP URL. Used as a return value for
 |  :func:`parse_url`. Both the scheme and host are normalized as they are
 |  both case-insensitive according to RFC 3986.
 |  
 |  Method resolution order:
 |      Url
 |      Url
 |      builtins.tuple
 |      builtins.object

Upvotes: 1

Steven
Steven

Reputation: 2133

urlpath is my go-to for a URL object. It mirrors the pathlib Path object.

Upvotes: 2

ssc
ssc

Reputation: 9903

~10 years late to the party here, but today, pydantic provides several URL types that might be helpful for validating, storing and passing around URLs; with type hints and mypy becoming more and more prevalent nowadays, some might consider this some kind of standard.

Upvotes: 9

George Sovetov
George Sovetov

Reputation: 5238

What we have as of 2018:

Only furl is being maintained today but its major disadvantage is that it's mutable, that doesn't encourage best practices, of course. (There is good modern reference — pathlib which consists of immutable classes.)

Overall, having a painless OO way to parse and construct URLs is graeat.

Update

yarl is worth looking at.

Upvotes: 5

Fred Foo
Fred Foo

Reputation: 363757

urlparse does encapsulate URLs into a class, called ParseResult, so it can be considered a factory function for these. Straight from the Python docs:

>>> urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')

If you desperately want a class called URL to encapsulate your URLs, use an alias (URL = urlparse.ParseResult) or create an adapter.

Upvotes: 28

neutrinus
neutrinus

Reputation: 2009

You might want consider having a look at furl because it might be an answer to your needs.

Upvotes: 7

Related Questions