I found another (simpler) solution here: http://www.piware.de/2011/01/creating-an-https-server-in-python/

This is my working porting to python3:

from http.server import HTTPServer,SimpleHTTPRequestHandler
from socketserver import BaseServer
import ssl

httpd = HTTPServer(('localhost', 1443), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='certificate.pem', server_side=True)
httpd.serve_forever()
Answer from Antonio Ragagnin on Stack Overflow
🌐
GitHub
github.com › vmware › pyvmomi › issues › 1057
Python 3.12 issue with wrap_socket (deprecated in 3.7) · Issue #1057 · vmware/pyvmomi
November 21, 2023 - Remove the ssl.wrap_socket() function, deprecated in Python 3.7: instead, create a ssl.SSLContext object and call its ssl.SSLContext.wrap_socket method. Any package that still uses ssl.wrap_socket() is broken and insecure.
Author   ekrichbaum
🌐
Python
docs.python.org › 3 › library › ssl.html
ssl — TLS/SSL wrapper for socket objects — Python 3.14.4 ...
Deprecated since version 3.10: All TLSVersion members except TLSVersion.TLSv1_2 and TLSVersion.TLSv1_3 are deprecated. ... However, since the SSL (and TLS) protocol has its own framing atop of TCP, the SSL sockets abstraction can, in certain ...
🌐
GitHub
github.com › celery › py-amqp › issues › 303
ssl.wrap_socket() is deprecated · Issue #303 · celery/py-amqp
January 17, 2020 - sock = ssl.wrap_socket(**opts) is deprecated from python 3.7 (see [1]).
Author   matusvalo
🌐
Trac Project
trac.edgewall.org › ticket › 13749
#13749 (Remove the ssl.wrap_socket() function, deprecated in Python 3.7) – The Trac Project
Serving on 0.0.0.0:3000 view at https://127.0.0.1:3000/ Using HTTP/1.1 protocol version Traceback (most recent call last): File "/home/jun66j5/src/tracdev/git/trac/web/standalone.py", line 392, in <module> main() File "/home/jun66j5/src/tracdev/git/trac/web/standalone.py", line 381, in main serve() File "/home/jun66j5/src/tracdev/git/trac/web/standalone.py", line 344, in serve httpd.socket = ssl.wrap_socket(httpd.socket, server_side=True, ^^^^^^^^^^^^^^^ AttributeError: module 'ssl' has no attribute 'wrap_socket' Fixed in [17784] and merged in [17785]. Note: See TracTickets for help on using tickets.
🌐
GitHub
github.com › web-platform-tests › wpt › issues › 44427
DeprecationWarning: ssl.wrap_socket() is deprecated, use SSLContext.wrap_socket() · Issue #44427 · web-platform-tests/wpt
February 6, 2024 - DeprecationWarning: ssl.wrap_socket() is deprecated, use SSLContext.wrap_socket()#44427 · Copy link · Labels · infrawptserve · gsnedders · opened · on Feb 6, 2024 · Issue body actions · We don't seem to have had any tests for the H1 ...
Author   gsnedders
🌐
GitHub
github.com › fkie-cad › socbed › issues › 89
Replace fully deprecated ssl.wrap_socket() with ssl.SSLContext.wrap_socket() (Python 3.12+) · Issue #89 · fkie-cad/socbed
October 14, 2024 - The ssl.wrap_socket() method, which has been deprecated in Python 3.7, has now been removed in Python 3.12. Consequently, our vmconsole no longer works, and we need to update our code accordingly. ...
Author   Maspital
🌐
Guardrails
docs.guardrails.io › vulnerabilities › insecure network communication
Insecure Network Communication | GuardRails
February 25, 2023 - Since Python 3.2 and 2.7.9, it is recommended to use the SSLContext.wrap_socket() instead of ssl.wrap_socket().
Find elsewhere
🌐
GitLab
gitlab.com › hartmut goebel › managesieve › #8
ssl.wrap_socket() function, deprecated in Python 3.7 (#8) · Issues · Hartmut Goebel / managesieve · GitLab
January 6, 2023 - Hi, With Python 3.12. https://docs.python.org/3.12/whatsnew/3.12.html > Remove the ssl.wrap_socket() function, deprecated in Python 3.7: instead, create a ssl.SSLContext object and call its ssl.SSLContext.wrap_socket method.
🌐
GitHub
github.com › zyn3rgy › LdapRelayScan › pull › 14
ssl.wrap_socket deprecation fix by evanmiller2112 · Pull Request #14 · zyn3rgy/LdapRelayScan
This just implements the SSLContext.wrap_socket function as recommended in the Python spec: Deprecated since version 3.7: Since Python 3.2 and 2.7.9, it is recommended to use the SSLContext.wrap_socket() instead of wrap_socket().
Author   zyn3rgy
Top answer
1 of 1
1

Per the mysql-connector-python package bug report linked by steeldriver, the cause of the issue appears to be that Python 3.12 removed ssl.wrap_socket (it had been marked deprecated since Python 3.7), but the version of the Python mysql.connector package provided by Ubuntu's python3-mysql.connector package is an old version incompatible with Python 3.12 that expects ssl.wrap_socket to still exist.

From the upstream mysql-connector-python repo, it appears that support for Python 3.12 was first introduced in version v8.2.0. But, in Ubuntu 24.04 LTS, if we apt install python3-msql.connector, inspecting /usr/lib/python3/dist-packages/mysql/connector/version.py and /usr/lib/python3/dist-packages/mysql/connector/network.py shows that it is version v8.0.15, which is not documented as supporting Python 3.12, and attempts to call ssl.wrap_socket are still present inside network.py. So we expect that not to work.

Here's an example following Steeldriver's suggested workaround, avoiding use of the globally installed mysql.connector package provided by ubuntu's python3-mysql.connector package that is incompatible with python3.12, and instead installing a newer version of that package from pypi to an isolated python virtual environment:

$ cat issue.sh

#! /usr/bin/env bash
set -uxo pipefail

python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade mysql-connector-python
python3 test.py
$ cat test.py
import mysql.connector
db = mysql.connector.connect(host="localhost", user="ray", password="...")
print(db)
exit(0)

Run it with chmod +x ./issue.sh && ./issue.sh.

When I run this, it installs version 9.5.0 of the msql-connector-python package.

🌐
Red Hat
bugzilla.redhat.com › show_bug.cgi
1565943 – Don't recommend ssl.wrap_socket()
April 11, 2018 - RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red ...
🌐
GitHub
github.com › OWASP › Nettacker › issues › 1190
Python 3.12 SSL compatibility: ssl.wrap_socket removed · Issue #1190 · OWASP/Nettacker
December 25, 2025 - The deprecated ssl.wrap_socket() was removed in Python 3.12.
Author   deekshithaby
🌐
GitHub
github.com › celery › py-amqp › pull › 327
Stop to use deprecated method ssl.wrap_socket by 4383 · Pull Request #327 · celery/py-amqp
The top-level function is limited and creates an insecure client socket without server name indication or hostname matching [1]. Python 2.7 is now officially unmaintained, latest version of python 2.7 is 2.7.18, py-amqp only support python versions who are compatible with these changes [2]. These changes move away from ssl.wrap_socket by using now ssl.SSLContext.wrap_socket [3].
Author   celery
🌐
GitHub
github.com › zyn3rgy › LdapRelayScan › issues › 9
Deprecation Warning `ssl.wrap_socket()` · Issue #9 · zyn3rgy/LdapRelayScan
May 5, 2022 - On Python 3.10.4 I get the following deprecation warning when running the tool. LdapRelayScan.py:121: DeprecationWarning: ssl.wrap_socket() is deprecated, use SSLContext.wrap_socket()
Author   exploide
🌐
GitHub
github.com › eventlet › eventlet › issues › 795
[Python 3.12] AttributeError: module 'ssl' has no attribute 'wrap_socket' · Issue #795 · eventlet/eventlet
April 3, 2023 - From What's new: Remove the ssl.wrap_socket() function, deprecated in Python 3.7: instead, create a ssl.SSLContext object and call its ssl.SSLContext.wrap_socket method. Any package that still uses ssl.wrap_socket() is broken and insecure.
Author   hrnciar
🌐
GitHub
github.com › pajbot › pajbot › issues › 2795
`ssl.wrap_socket` removed in Python 3.12 · Issue #2795 · pajbot/pajbot
January 2, 2025 - Remove the ssl.wrap_socket() function, deprecated in Python 3.7: instead, create a ssl.SSLContext object and call its ssl.SSLContext.wrap_socket method. Any package that still uses ssl.wrap_socket() is broken and insecure.
Author   IS2511
🌐
Pythontic
pythontic.com › ssl › sslcontext › wrap_socket
wrap_socket() method of SSLContext class in Python | Pythontic.com
Given a connection oriented socket, the SSLContext.wrap_socket() method returns an SSLSocket. The method copies the attributes/options of the socket instance and creates an SSLSocket. The original socket is detached.