| 1 |
v2 Migration Guide |
| 2 |
==================== |
| 3 |
|
| 4 |
**urllib3 2.x is now available!** Read below for how to get started and what is contained in the new major release. |
| 5 |
|
| 6 |
**🚀 Migrating from 1.x to 2.x** |
| 7 |
-------------------------------- |
| 8 |
|
| 9 |
We're maintaining **functional API compatibility for most users** to make the |
| 10 |
migration an easy choice for almost everyone. Most changes are either to default |
| 11 |
configurations, supported Python versions, or internal implementation details. |
| 12 |
So unless you're in a specific situation you should notice no changes! 🎉 |
| 13 |
|
| 14 |
.. note:: |
| 15 |
|
| 16 |
If you have difficulty migrating to 2.x or following this guide |
| 17 |
you can `open an issue on GitHub <https://github.com/urllib3/urllib3/issues>`_ |
| 18 |
or reach out in `our community Discord channel <https://discord.gg/urllib3>`_. |
| 19 |
|
| 20 |
|
| 21 |
What are the important changes? |
| 22 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 |
|
| 24 |
Here's a short summary of which changes in urllib3 2.x are most important: |
| 25 |
|
| 26 |
- Python version must be **3.10 or later** (previously supported Python 2.7, and 3.5 to 3.9). |
| 27 |
- Removed support for non-OpenSSL TLS libraries (like LibreSSL and wolfSSL). |
| 28 |
- Removed support for OpenSSL versions older than 1.1.1. |
| 29 |
- Removed support for Python implementations that aren't CPython or PyPy3 (previously supported Google App Engine, Jython). |
| 30 |
- Removed the ``urllib3.contrib.ntlmpool`` module. |
| 31 |
- Removed the ``urllib3.contrib.securetransport`` module. |
| 32 |
- Removed the ``urllib3[secure]`` extra. |
| 33 |
- Deprecated URLs without a scheme (ie 'https://') and will be raising an error in a future version of urllib3. |
| 34 |
- Changed the default minimum TLS version to TLS 1.2 (previously was TLS 1.0). |
| 35 |
- Changed the default request body encoding from 'ISO-8859-1' to 'UTF-8'. |
| 36 |
- Removed support for verifying certificate hostnames via ``commonName``, now only ``subjectAltName`` is used. |
| 37 |
- Removed the default set of TLS ciphers, instead now urllib3 uses the list of ciphers configured by the system. |
| 38 |
|
| 39 |
For a full list of changes you can look at `the changelog <https://github.com/urllib3/urllib3/blob/main/CHANGES.rst>`_. |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
Sunsetting urllib3 1.26.x |
| 44 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 45 |
|
| 46 |
urllib3 1.26.x is not currently maintained. urllib3 2.x is the best version of urllib3 |
| 47 |
and is widely supported by the larger Python ecosystem. That said, urllib3 1.26.x still |
| 48 |
sees significant download numbers, mainly because the botocore package still requires |
| 49 |
urllib3 1.26.x for Python 3.9 and earlier. If your organization would benefit from the |
| 50 |
continued support of urllib3 1.26.x, please contact [email protected] to |
| 51 |
discuss sponsorship or contribution opportunities. |
| 52 |
|
| 53 |
However, upgrading is still recommended as **no new feature developments or |
| 54 |
fixes will be shipped to the 1.26.x release stream**. |
| 55 |
|
| 56 |
**🤔 Common upgrading issues** |
| 57 |
------------------------------ |
| 58 |
|
| 59 |
ssl module is compiled with OpenSSL 1.0.2.k-fips |
| 60 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 61 |
|
| 62 |
.. code-block:: text |
| 63 |
|
| 64 |
ImportError: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips 26 Jan 2017'. |
| 65 |
See: https://github.com/urllib3/urllib3/issues/2168 |
| 66 |
|
| 67 |
Remediation depends on your system: |
| 68 |
|
| 69 |
- **AWS Lambda**: Upgrade to the Python 3.10 (or later) runtime as it uses OpenSSL 1.1.1. Alternatively, you can |
| 70 |
use a `custom Docker image |
| 71 |
<https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/>`_ and ensure you |
| 72 |
use a Python build that uses OpenSSL 1.1.1 or later. |
| 73 |
- **Amazon Linux 2**: Upgrade to `Amazon Linux 2023 |
| 74 |
<https://aws.amazon.com/linux/amazon-linux-2023/>`_. Alternatively, you can install OpenSSL 1.1.1 |
| 75 |
on Amazon Linux 2 using ``yum install openssl11 openssl11-devel`` and then install Python with a |
| 76 |
tool like pyenv. |
| 77 |
- **Red Hat Enterpritse Linux 7 (RHEL 7)**: Upgrade to RHEL 8 or later. |
| 78 |
- **Read the Docs**: Upgrade your `configuration file to use Ubuntu 22.04 |
| 79 |
<https://docs.readthedocs.io/en/stable/config-file/v2.html>`_ by using ``os: ubuntu-22.04`` in the |
| 80 |
``build`` section. Feel free to use the `urllib3 configuration |
| 81 |
<https://github.com/urllib3/urllib3/blob/2.0.0/.readthedocs.yml>`_ as an inspiration. |
| 82 |
|
| 83 |
docker.errors.dockerexception: error while fetching server api version: request() got an unexpected keyword argument 'chunked' |
| 84 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 85 |
|
| 86 |
Upgrade to ``docker==6.1.0`` that is compatible with urllib3 2.0. |
| 87 |
|
| 88 |
ImportError: cannot import name 'gaecontrib' from 'requests_toolbelt._compat' |
| 89 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 90 |
|
| 91 |
To be compatible with urllib3 2.0, Requests Toolbelt released version 1.0.0 without Google App |
| 92 |
Engine Standard Python 2.7 support. Most users that reported this issue were using the `Pyrebase |
| 93 |
<https://github.com/thisbejim/Pyrebase>`_ library that provides an API for the Firebase API. This |
| 94 |
library is unmaintained, but `replacements exist |
| 95 |
<https://github.com/thisbejim/Pyrebase/issues/435>`_. |
| 96 |
|
| 97 |
``ImportError: cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_'`` |
| 98 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 99 |
|
| 100 |
This likely happens because you're using botocore which `does not support urllib3 2.0 yet |
| 101 |
<https://github.com/boto/botocore/issues/2921>`_. The good news is that botocore explicitly declares |
| 102 |
in its dependencies that it only supports ``urllib3<2``. Make sure to use a recent pip. That way, pip |
| 103 |
will install urllib3 1.26.x for versions of botocore that do not support urllib3 2.0. |
| 104 |
|
| 105 |
If you're deploying to an AWS environment such as Lambda with the Python 3.9 runtime or a host |
| 106 |
using Amazon Linux 2, you'll need to explicitly pin to ``urllib3<2`` in your project to ensure |
| 107 |
urllib3 2.0 isn't brought into your environment. Otherwise, this may result in unintended side |
| 108 |
effects with the default boto3 installation. |
| 109 |
|
| 110 |
AttributeError: module 'urllib3.connectionpool' has no attribute 'VerifiedHTTPSConnection' |
| 111 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 112 |
|
| 113 |
The ``VerifiedHTTPSConnection`` class has always been documented to be in the |
| 114 |
:mod:`~urllib3.connection` module. It used to be possible to import it from |
| 115 |
:mod:`~urllib3.connectionpool` but that was acccidental and is no longer possible due to a |
| 116 |
refactoring in urllib3 2.0. |
| 117 |
|
| 118 |
Note that the new name of this class is :class:`~urllib3.connection.HTTPSConnection`. It can be used |
| 119 |
starting from urllib3 1.25.9. |
| 120 |
|
| 121 |
AttributeError: 'HTTPResponse' object has no attribute 'strict' |
| 122 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 123 |
|
| 124 |
The ``strict`` parameter is unneeded with Python 3 and should be removed. |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
Migrating as an application developer? |
| 129 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 130 |
|
| 131 |
If you're someone who writes Python but doesn't ship as a package (things like web services, data science, tools, and more) this section is for you. |
| 132 |
|
| 133 |
Python environments only allow for one version of a dependency to be installed per environment which means |
| 134 |
that **all of your dependencies using urllib3 need to support 2.x for you to upgrade**. |
| 135 |
|
| 136 |
The best way to visualize relationships between your dependencies is using `pipdeptree <https://pypi.org/project/pipdeptree>`_ and ``$ pipdeptree --reverse``: |
| 137 |
|
| 138 |
.. code-block:: bash |
| 139 |
|
| 140 |
# From inside your Python environment: |
| 141 |
$ python -m pip install pipdeptree |
| 142 |
# We only care about packages requiring urllib3 |
| 143 |
$ pipdeptree --reverse | grep "requires: urllib3" |
| 144 |
├── botocore==1.38.36 [requires: urllib3>=1.25.4,<1.27] |
| 145 |
└── requests==2.32.4 [requires: urllib3>=1.21.1,<3] |
| 146 |
|
| 147 |
Reading the output from above, there are two packages which depend on urllib3: ``botocore`` and ``requests``. |
| 148 |
While requests supports urllib3 2.x (with the ``<3`` specified), botocore requires urllib3 1.26.x (with the ``<1.27`` version specifier). |
| 149 |
Note that botocore does support urllib3 2.x, but it only supports it on Python 3.10 and later, which mandates OpenSSL 1.1.1+. |
| 150 |
|
| 151 |
It's important to know `urllib3 does not receive security fixes at the moment <#sunsetting-urllib3-1-26-x>`. |
| 152 |
|
| 153 |
|
| 154 |
Migrating as a package maintainer? |
| 155 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 156 |
|
| 157 |
If you're a maintainer of a package that uses urllib3 under the hood then this section is for you. |
| 158 |
You may have already seen an issue opened from someone on our team about the upcoming release. |
| 159 |
|
| 160 |
The primary goal for migrating to urllib3 2.x should be to ensure your package supports **both urllib3 v1.26.x and 2.x for some time**. |
| 161 |
This is to reduce the chance that diamond dependencies are introduced into your users' dependencies which will then cause issues |
| 162 |
with them upgrading to the latest version of **your package**. |
| 163 |
|
| 164 |
The first step to supporting urllib3 2.x is to make sure the version 2.x not being excluded by ``install_requires``. You should |
| 165 |
ensure your package allows for both urllib3 1.26.x and 2.x to be used: |
| 166 |
|
| 167 |
.. code-block:: python |
| 168 |
|
| 169 |
# setup.py (setuptools) |
| 170 |
setup( |
| 171 |
... |
| 172 |
install_requires=["urllib3>=1.26,<3"] |
| 173 |
) |
| 174 |
|
| 175 |
# pyproject.toml (hatch) |
| 176 |
[project] |
| 177 |
dependencies = [ |
| 178 |
"urllib3>=1.26,<3" |
| 179 |
] |
| 180 |
|
| 181 |
Next you should try installing urllib3 v2.x locally and run your test suite. |
| 182 |
|
| 183 |
.. code-block:: bash |
| 184 |
|
| 185 |
$ python -m pip install -U 'urllib3>=2' |
| 186 |
|
| 187 |
Because there are new ``FutureWarnings`` you should ensure that you're |
| 188 |
able to see those warnings when running your test suite. To do so you can add |
| 189 |
the following to your test setup to ensure even ``FutureWarnings`` are |
| 190 |
output to the terminal: |
| 191 |
|
| 192 |
.. code-block:: bash |
| 193 |
|
| 194 |
# Set PYTHONWARNING=default to show all warnings. |
| 195 |
$ export PYTHONWARNINGS="default" |
| 196 |
|
| 197 |
# Run your test suite and look for failures. |
| 198 |
# Pytest automatically prints all warnings. |
| 199 |
$ pytest tests/ |
| 200 |
|
| 201 |
or you can opt-in within your Python code: |
| 202 |
|
| 203 |
.. code-block:: python |
| 204 |
|
| 205 |
# You can change warning filters according to the filter rules: |
| 206 |
# https://docs.python.org/3/library/warnings.html#warning-filter |
| 207 |
import warnings |
| 208 |
warnings.filterwarnings("default", category=FutureWarning) |
| 209 |
|
| 210 |
Any failures or deprecation warnings you receive should be fixed as urllib3 v3.0 will remove all |
| 211 |
deprecated features. Many deprecation warnings will make suggestions about what to do to avoid the deprecated feature. |
| 212 |
|
| 213 |
Warnings will look something like this: |
| 214 |
|
| 215 |
.. code-block:: bash |
| 216 |
|
| 217 |
FutureWarning: 'ssl_version' option is deprecated and will be removed |
| 218 |
in urllib3 v3.0. Instead use 'ssl_minimum_version' |
| 219 |
|
| 220 |
Continue removing deprecation warnings until there are no more. After this you can publish a new release of your package |
| 221 |
that supports both urllib3 1.26.x and 2.x. |
| 222 |
|
| 223 |
.. note:: |
| 224 |
|
| 225 |
If you're not able to support both 1.26.x and 2.x of urllib3 at the same time with your package please |
| 226 |
`open an issue on GitHub <https://github.com/urllib3/urllib3/issues>`_ or reach out in |
| 227 |
`our community Discord channel <https://discord.gg/urllib3>`_. |
| 228 |
|
| 229 |
|
| 230 |
**💪 User-friendly features** |
| 231 |
----------------------------- |
| 232 |
|
| 233 |
urllib3 has always billed itself as a **user-friendly HTTP client library**. |
| 234 |
In the spirit of being even more user-friendly we've added two features |
| 235 |
which should make using urllib3 for tinkering sessions, throw-away scripts, |
| 236 |
and smaller projects a breeze! |
| 237 |
|
| 238 |
urllib3.request() |
| 239 |
~~~~~~~~~~~~~~~~~ |
| 240 |
|
| 241 |
Previously the highest-level API available for urllib3 was a ``PoolManager``, |
| 242 |
but for many cases configuring a poolmanager is extra steps for no benefit. |
| 243 |
To make using urllib3 as simple as possible we've added a top-level function |
| 244 |
for sending requests from a global poolmanager instance: |
| 245 |
|
| 246 |
.. code-block:: python |
| 247 |
|
| 248 |
>>> import urllib3 |
| 249 |
>>> resp = urllib3.request("GET", "https://example.com") |
| 250 |
>>> resp.status |
| 251 |
200 |
| 252 |
|
| 253 |
JSON support for requests and responses |
| 254 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 255 |
|
| 256 |
JSON is everywhere – and now it's in urllib3, too! |
| 257 |
|
| 258 |
If you'd like to send JSON in a request body or deserialize a response body |
| 259 |
from JSON into Python objects you can now use the new ``json=`` parameter |
| 260 |
for requests and ``HTTPResponse.json()`` method on responses: |
| 261 |
|
| 262 |
.. code-block:: python |
| 263 |
|
| 264 |
import urllib3 |
| 265 |
|
| 266 |
# Send a request with a JSON body. |
| 267 |
# This adds 'Content-Type: application/json' by default. |
| 268 |
resp = urllib3.request( |
| 269 |
"POST", "https://example.api.com", |
| 270 |
json={"key": "value"} |
| 271 |
) |
| 272 |
|
| 273 |
# Receive a JSON body in the response. |
| 274 |
resp = urllib3.request("GET", "https://xkcd.com/2347/info.0.json") |
| 275 |
|
| 276 |
# There's always an XKCD... |
| 277 |
resp.json() |
| 278 |
{ |
| 279 |
"num": 2347, |
| 280 |
"img": "https://imgs.xkcd.com/comics/dependency.png", |
| 281 |
"title": "Dependency", |
| 282 |
... |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
**✨ Optimized for Python 3.10+** |
| 287 |
--------------------------------- |
| 288 |
|
| 289 |
urllib3 2.x specifically targets CPython 3.10+ and PyPy v7.3.20+ (compatible with CPython 3.11) |
| 290 |
and dropping support for Python versions 2.7, and 3.5 to 3.9. |
| 291 |
|
| 292 |
By dropping end-of-life Python versions we're able to optimize |
| 293 |
the codebase for Python 3.10+ by using new features to improve |
| 294 |
performance and reduce the amount of code that needs to be executed |
| 295 |
in order to support legacy versions. |
| 296 |
|
| 297 |
|
| 298 |
**📜 Type-hinted APIs** |
| 299 |
----------------------- |
| 300 |
|
| 301 |
You're finally able to run Mypy or other type-checkers |
| 302 |
on code using urllib3. This also means that for IDEs |
| 303 |
that support type hints you'll receive better suggestions |
| 304 |
from auto-complete. No more confusion with ``**kwargs``! |
| 305 |
|
| 306 |
We've also added API interfaces like ``BaseHTTPResponse`` |
| 307 |
and ``BaseHTTPConnection`` to ensure that when you're sub-classing |
| 308 |
an interface you're only using supported public APIs to ensure |
| 309 |
compatibility and minimize breakages down the road. |
| 310 |
|
| 311 |
.. note:: |
| 312 |
|
| 313 |
If you're one of the rare few who is subclassing connections |
| 314 |
or responses you should take a closer look at detailed changes |
| 315 |
in `the changelog <https://github.com/urllib3/urllib3/blob/main/CHANGES.rst>`_. |
| 316 |
|
| 317 |
|
| 318 |
**🔐 Modern security by default** |
| 319 |
--------------------------------- |
| 320 |
|
| 321 |
HTTPS requires TLS 1.2+ |
| 322 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 323 |
|
| 324 |
Greater than 95% of websites support TLS 1.2 or above. |
| 325 |
At this point we're comfortable switching the default |
| 326 |
minimum TLS version to be 1.2 to ensure high security |
| 327 |
for users without breaking services. |
| 328 |
|
| 329 |
Dropping TLS 1.0 and 1.1 by default means you |
| 330 |
won't be vulnerable to TLS downgrade attacks |
| 331 |
if a vulnerability in TLS 1.0 or 1.1 were discovered in |
| 332 |
the future. Extra security for free! By dropping TLS 1.0 |
| 333 |
and TLS 1.1 we also tighten the list of ciphers we need |
| 334 |
to support to ensure high security for data traveling |
| 335 |
over the wire. |
| 336 |
|
| 337 |
If you still need to use TLS 1.0 or 1.1 in your application |
| 338 |
you can still upgrade to v2.x, you'll only need to set |
| 339 |
``ssl_minimum_version`` to the proper value to continue using |
| 340 |
legacy TLS versions. |
| 341 |
|
| 342 |
|
| 343 |
Stop verifying commonName in certificates |
| 344 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 345 |
|
| 346 |
Dropping support the long deprecated ``commonName`` |
| 347 |
field on certificates in favor of only verifying |
| 348 |
``subjectAltName`` to put us in line with browsers and |
| 349 |
other HTTP client libraries and to improve security for our users. |
| 350 |
|
| 351 |
|
| 352 |
Certificate verification via SSLContext |
| 353 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 354 |
|
| 355 |
By default certificate verification is handled by urllib3 |
| 356 |
to support legacy Python versions, but now we can |
| 357 |
rely on Python's certificate verification instead! This |
| 358 |
should result in a speedup for verifying certificates |
| 359 |
and means that any improvements made to certificate |
| 360 |
verification in Python or OpenSSL will be immediately |
| 361 |
available. |