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
- Control nodeUbuntu 26.04 LTS
- Python3.14.4
- requests2.34.2
- paramiko5.0.0
- TimeAbout 16 min
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.
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 GB |
This guide includes
Use this on a locked-down host where you cannot add a dependency. This matters because an HTTP error arrives as an exception that is also a response - the biggest behavioural difference from requests, and the one that breaks ported code.
- proving a script needs nothing installed
- making a GET with
urlopen, and encoding the query yourself - finding that an error is an exception which is also a response
- posting with headers, including Authorization
- wrapping it once, and seeing what you are not getting
Before you start
-
Proving a script needs nothing installed
-
A GET with urlopen
-
Query parameters, and the encoding you now do yourself
-
An error is an exception that is also a response
-
A POST, with headers
-
The Authorization header
-
So wrap it once
-
And what you are not getting