from Hacker News

Python HTTP library 'urllib3' now works in the browser

by SethMLarson on 1/30/24, 5:05 PM with 32 comments

  • by codingglass on 1/31/24, 1:53 AM

    This a a great example of good work getting 'upstreamed.' The go-to solution for using requests and urllib3 in the browser had been Koen Vossen's 'pyodide-http' package, which you'd install in PyScript/Pyodide and then call 'pyodide_http.patch_all()`. It monkey-patched requests and urllib3 to use the browser API's. [1]

    Then 'pyodide-http' got adopted into the Pyodide-included packages[2], so you didn't have to specify it for install, just import it and run patch_all().

    And now a similar technique has been incorporated directly into urllib3. (Not a direct port - as far as I know Joe Marshall and others did a full reimplementation so that urllib3's API's would be maintained as much as possible). [2]

    Very cool.

    Edit: Looks like there's a note to this same effect on Seth Larson's post about urllib3's future. [3]

    [1] https://github.com/koenvo/pyodide-http

    [2] https://pyodide.org/en/stable/usage/packages-in-pyodide.html

    [3] https://sethmlarson.dev/urllib3-is-fundraising-for-http2-sup...

  • by Austizzle on 1/30/24, 9:41 PM

    Incredible! Two years ago I was experimenting in porting some code at the company I was at to python in the browser (instead of QT desktop apps) and this was the biggest thing I had to work around! I had to manually monkey patch or proxy a few libraries to make them work, but I'll have to test out this update and see if everything magically works
  • by Extigy on 1/30/24, 6:52 PM

    Since this is using fetch/XHR under the hood, I guess requests from the browser are restricted only to same-origin URLs or servers responding with permissive CORS headers?
  • by canadiantim on 1/30/24, 5:12 PM

    Pyodide examples using urllib3 in browser still requires using fetch, so this still requires javascript.

    What does this practically mean?

    Does this ever point to a future where we can use urlib3 instead of fetch?

  • by hoodchatham on 1/31/24, 3:43 AM

    aiohttp is still work in progress but it works with a monkey patch. https://github.com/aio-libs/aiohttp/pull/7803
  • by hpeter on 1/30/24, 6:41 PM

    why would you want to run python in the browser? Anyone can ELI5?