I've been trying to convert a Python app that I made into a webapp so that it can be easily used by others. Part of the app involves users entering their username and a request being sent to an api with their username. I'm using the boilerplate website provided by PyScript to test things out.
index.html
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title> Polyglot - Piratical PyScript</title>
<script type="module" src="https://pyscript.net/releases/2023.11.2/core.js"></script>
</head>
<body>
<h1>Polyglot ➡️ ☠️</h1>
<p>Translate English into Pirate speak...</p>
<input type="text" id="user" placeholder="Type English here..." />
<button py-click="find_game">Find Game</button>
<div id="output"></div>
<script type="py" src="./test.py" config="./pyscript.toml"></script>
</body>
</html>
test.py
import requests
from pyscript import document
def find_game(event):
input_text = document.querySelector("#user")
username = input_text.value
custom_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}
last_game = requests.get("https://httpbin.org/headers", headers=custom_headers)
output_div = document.querySelector("#output")
output_div.innerText = last_game
pyscript.toml
packages = ["requests", "ssl"]
When I start a static webserver using python3 -m http.server
and press the Find Game button, the following error pops up.
Traceback (most recent call last):
File "/lib/python3.11/site-packages/urllib3/connection.py", line 203, in _new_conn
sock = connection.create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/urllib3/util/connection.py", line 85, in create_connection
raise err
File "/lib/python3.11/site-packages/urllib3/util/connection.py", line 67, in create_connection
_set_socket_options(sock, socket_options)
File "/lib/python3.11/site-packages/urllib3/util/connection.py", line 100, in _set_socket_options
sock.setsockopt(*opt)
OSError: [Errno 50] Protocol not available
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/lib/python3.11/site-packages/urllib3/connectionpool.py", line 790, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/urllib3/connectionpool.py", line 491, in _make_request
raise new_e
File "/lib/python3.11/site-packages/urllib3/connectionpool.py", line 467, in _make_request
self._validate_conn(conn)
File "/lib/python3.11/site-packages/urllib3/connectionpool.py", line 1096, in _validate_conn
conn.connect()
File "/lib/python3.11/site-packages/urllib3/connection.py", line 611, in connect
self.sock = sock = self._new_conn()
^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/urllib3/connection.py", line 218, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0xd90f88>: Failed to establish a new connection: [Errno 50] Protocol not available
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/lib/python3.11/site-packages/requests/adapters.py", line 486, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/urllib3/connectionpool.py", line 844, in urlopen
retries = retries.increment(
^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/urllib3/util/retry.py", line 515, in increment
raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /headers (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0xd90f88>: Failed to establish a new connection: [Errno 50] Protocol not available'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<exec>", line 58, in find_game
File "/lib/python3.11/site-packages/requests/api.py", line 73, in get
return request("get", url, params=params, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/requests/api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lib/python3.11/site-packages/requests/adapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /headers (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0xd90f88>: Failed to establish a new connection: [Errno 50] Protocol not available'))
Most of the similar posts I've come across online talk about Dockers, something I'm not too familiar about so I'm not sure whether the issue is with the way PyScript is implemented or my code.
I've also tried using other libraries to make HTTP requests such as urllib3 but the same error occurs.