Intern
237
What is a URI?
Answer from AI
sobes.tech AI
URI (Uniform Resource Identifier) is a sequence of characters that identifies an abstract or physical resource. A URI can be classified as a URL (Uniform Resource Locator), which provides means to locate it, or a URN (Uniform Resource Name), which assigns a unique name within a specific namespace.
Structure of URI:
A URI typically consists of the following components:
- Scheme: Defines the protocol or access mechanism to the resource (e.g.,
http,https,ftp,file). - Authority: Contains information about the source of authorization, usually in the format
[userinfo@]host[:port]. - Path: Specifies the particular resource within the authority.
- Query: Contains additional data for the request, presented as key-value pairs separated by
&. - Fragment: Identifies a secondary resource.
Differences between URI, URL, and URN:
| Type | Description | Example |
|---|---|---|
| URI | Resource identifier (locator OR name) | urn:isbn:0-486-27557-4 |
| URL | Resource locator | https://www.example.com/page.html |
| URN | Resource name | urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6 |
- URL indicates how to access the resource and its location.
- URN provides a persistent, unique name for the resource regardless of its location.
- URI is a generalization of both URL and URN.
In Python, the urllib.parse module is often used for working with URI/URL.
# Example of parsing a URL string
from urllib.parse import urlparse
uri_string = "https://user:pass@www.example.com:8080/path/to/resource?query=value#fragment"
parsed_uri = urlparse(uri_string)
# Accessing components
print(parsed_uri.scheme) # Output: https
print(parsed_uri.netloc) # Output: user:pass@www.example.com:8080
print(parsed_uri.path) # Output: /path/to/resource
print(parsed_uri.query) # Output: query=value
print(parsed_uri.fragment) # Output: fragment