Python urllib.request Module

The urllib.request module lets you fetch data from URLs using the Python standard library.

from urllib import request

For many projects, third-party libraries such as Requests are more convenient. Use urllib.request when you want a standard-library-only solution.

Opening a URL

urlopen returns a response object.

from urllib import request

response = request.urlopen('data:text/plain,Hello%20Python')
print(response.headers.get_content_type())
text/plain

Reading response data

Response bodies are returned as bytes.

from urllib import request

response = request.urlopen('data:text/plain,Hello%20Python')
content = response.read()
print(type(content).__name__)
print(content.decode('utf-8'))
bytes
Hello Python

Adding a timeout

When you open real network URLs, set a timeout so your program does not wait forever.

from urllib import request

response = request.urlopen('data:text/plain,ok', timeout=5)
print(response.read().decode('utf-8'))
ok

WARNING

Network calls can fail for many reasons. Handle exceptions such as urllib.error.URLError and be careful when fetching untrusted URLs.