ContrapunctusI
ContrapunctusI
CDCloudflare Developers
Created by ContrapunctusI on 2/24/2025 in #workers-help
urllib.request.urlopen will throw an exception with Python worker
If we use the following Python code which creates a connection with a website and returns its content:
from js import Response
import urllib.request

def on_fetch(request, env):
request = urllib.request.Request("https://example.org")
r = urllib.request.urlopen(request).read()
html = r.decode('utf-8')

return Response.new(html)
from js import Response
import urllib.request

def on_fetch(request, env):
request = urllib.request.Request("https://example.org")
r = urllib.request.urlopen(request).read()
html = r.decode('utf-8')

return Response.new(html)
Any attempt to request the worker will throw this weird error:
X [ERROR] Error in makeHandler

X [ERROR] PythonError: Traceback (most recent call last):
[...]
File "/lib/python312.zip/socket.py", line 837, in create_connection
sock.connect(sa)
BlockingIOError: [Errno 26] Operation in progress

During handling of the above exception, another exception occurred:
[...]
File "/lib/python312.zip/urllib/request.py", line 1392, in https_open
return self.do_open(http.client.HTTPSConnection, req,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lib/python312.zip/urllib/request.py", line 1347, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 26] Operation in progress>
[...]

[wrangler:inf] GET / 500 Internal Server Error (6444ms)
X [ERROR] Error in makeHandler

X [ERROR] PythonError: Traceback (most recent call last):
[...]
File "/lib/python312.zip/socket.py", line 837, in create_connection
sock.connect(sa)
BlockingIOError: [Errno 26] Operation in progress

During handling of the above exception, another exception occurred:
[...]
File "/lib/python312.zip/urllib/request.py", line 1392, in https_open
return self.do_open(http.client.HTTPSConnection, req,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lib/python312.zip/urllib/request.py", line 1347, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 26] Operation in progress>
[...]

[wrangler:inf] GET / 500 Internal Server Error (6444ms)
etc. (it's quite long) What is going on?
1 replies