From the Pyodide documentation,Pyodide is a Python distribution for the browser and Node.js based on WebAssembly andEmscripten.This technology also underpins thePyScript framework andJupyterlite, so should work in those environments too.
Starting in version 2.2.0 urllib3 supports being used in a Pyodide runtime utilizingtheJavaScript fetch APIor falling back onXMLHttpRequestif the fetch API isn’t available (such as when cross-origin isolationisn’t active). This means you can use Python libraries to make HTTP requests from your browser!
Because urllib3’s Emscripten support is API-compatible, this means thatlibraries that depend on urllib3 may now be usable from Emscripten and Pyodide environments, too.
Warning
Support for Emscripten and Pyodide is experimental. Report all bugs to theurllib3 issue tracker.Currently Node.js support is very experimental - see the description below.
It’s recommended torun Pyodide in a Web Workerin order to take full advantage of features like the fetch API which enables streaming of HTTP response bodies.
Using urllib3 with Pyodide means you need toget started with Pyodide first.The Pyodide project provides auseful online REPL to try in your browser withoutany setup or installation to test out the code examples below.
One minor note - when running Pyodide code from JavaScript, if you usepyodide.runPythonAsync ratherthanpyodide.runPython, urllib3 can sometimes run more efficiently. It is generally always worth usingrunPythonAsync.
urllib3’s Emscripten support is automatically enabled ifsys.platform is"emscripten", so no setup is required beyond installation and importing the module.
urllib3 is packaged with the default Pyodide build, so you should be able to use it as normal.
importurllib3resp=urllib3.request("GET","https://httpbin.org/anything")print(resp.status)# 200print(resp.headers)# HTTPHeaderDict(...)print(resp.json())# {"headers": {"Accept": "*/*", ...}, ...}
BecauseRequests is built on urllib3, Requests also works out of the box:
importrequestsresp=requests.request("GET","https://httpbin.org/anything")print(resp.status_code)# 200print(resp.headers)
Because we use JavaScript APIs under the hood, it’s not possible to use all of urllib3 features.Features which are usable with Emscripten support are:
Requests over HTTP and HTTPS
Timeouts
Retries
Streaming (with Web Workers and Cross-Origin Isolation)
Redirects (urllib3 controls redirects in Node.js but not in browsers where behavior is determined by runtime)
Decompressing response bodies
Features which don’t work with Emscripten:
Proxies, both forwarding and tunneling
Customizing TLS and certificates (uses browsers’ configuration)
Configuring low-level socket options or source address
To access the fetch API and do HTTP response streaming with urllib3you must be running the code within a Web Worker and set specific HTTP headersfor the serving website to enableCross-Origin Isolation.
You can verify whether a given environment is cross-origin isolated by evaluating the globalcrossOriginIsolated JavaScript property.
Node.js support uses a relatively new feature in WebAssembly known as JavaScript Promise Integration.To use urllib3 in Node.js, you need to use Node.js version 20 or newer and may need to call Node.js withthe--experimental-wasm-stack-switching command line parameter.