CertGrid CertGrid
Hands-on Lab·Python Automation for IT

Python urllib Without requests

Sometimes you cannot add a dependency: a locked-down host, a minimal image, a bootstrap script that has to run before pip exists. `urllib.request` does everything the last five guides did, with more code and two behavioural surprises - a 404 raises, and the exception it raises is also a readable response. This guide is the translation, and the honest list of what you give up.

APIs and Cloud Guide 29 of 39 Intermediate

Written against the versions above. `json.loads` accepts `bytes` from Python 3.6. `urllib.request` verifies TLS certificates by default from 3.4.3 onwards - `ssl._create_unverified_context` exists and should not be used.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

Before you start

  1. Proving a script needs nothing installed

  2. A GET with urlopen

  3. Query parameters, and the encoding you now do yourself

  4. An error is an exception that is also a response

  5. A POST, with headers

  6. The Authorization header

  7. So wrap it once

  8. And what you are not getting

Official sources