Skip to content

gh-85162: Add HTTPSServer to http.server #129607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 54 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
5bfc939
Add support HTTPS in http.server
donBarbos Feb 2, 2025
b382985
Correct style code
donBarbos Feb 2, 2025
4cc80c5
Add tests for HTTPSServer
donBarbos Feb 2, 2025
75fff2b
Update options
donBarbos Feb 3, 2025
64c3070
Update docs
donBarbos Feb 3, 2025
e4652a7
Merge branch 'main' into issue-85162
donBarbos Feb 3, 2025
abd949c
Revert "Correct style code"
donBarbos Feb 3, 2025
8fc2311
Merge branch 'main' into issue-85162
donBarbos Feb 3, 2025
4f587bd
Update docs and correct raising errors
donBarbos Feb 3, 2025
db796cd
Add helper method _create_context
donBarbos Feb 3, 2025
96d4a68
Update docs and replace password option
donBarbos Feb 4, 2025
947f581
Update Lib/http/server.py
donBarbos Feb 15, 2025
b8ba151
Update Doc/library/http.server.rst
donBarbos Feb 15, 2025
97e2032
Update Doc/library/http.server.rst
donBarbos Feb 15, 2025
bd97fd6
Update Doc/library/http.server.rst
donBarbos Feb 15, 2025
15b2581
Update Doc/library/http.server.rst
donBarbos Feb 15, 2025
1951e22
Update Doc/library/http.server.rst
donBarbos Feb 15, 2025
b4e1eba
Update Lib/http/server.py
donBarbos Feb 15, 2025
4838ff8
Update Lib/http/server.py
donBarbos Feb 15, 2025
3a7821f
Update Lib/http/server.py
donBarbos Feb 15, 2025
85ee1b5
Update Lib/http/server.py
donBarbos Feb 15, 2025
196e71d
Update Doc/library/http.server.rst
donBarbos Feb 15, 2025
efd44a4
Update Doc/library/http.server.rst
donBarbos Feb 15, 2025
4b33ecc
Update Doc/whatsnew/3.14.rst
donBarbos Feb 15, 2025
5fcc947
Update Doc/whatsnew/3.14.rst
donBarbos Feb 15, 2025
4df61de
Update Lib/http/server.py
donBarbos Feb 15, 2025
08a5720
Add suggestions
donBarbos Feb 15, 2025
43ae6b8
Merge branch 'main' into issue-85162
donBarbos Feb 15, 2025
6cff350
Update 2025-02-02-00-30-09.gh-issue-85162.BNF_aJ.rst
donBarbos Feb 15, 2025
0b2d50a
Update http.server.rst
donBarbos Feb 15, 2025
8a7f316
Move function back
donBarbos Feb 15, 2025
e7d9250
Add test case for pass certdata
donBarbos Feb 15, 2025
1b64e3d
Update test_httpservers.py
donBarbos Feb 15, 2025
c004b71
Update test_httpservers.py
donBarbos Feb 15, 2025
b6ba37f
Update test_httpservers.py
donBarbos Feb 15, 2025
bf86a0d
Update test_httpservers.py
donBarbos Feb 15, 2025
b89f4c4
Update test_httpservers.py
donBarbos Feb 15, 2025
c6879de
Update test_httpservers.py
donBarbos Feb 15, 2025
1ee542f
Update test_httpservers.py
donBarbos Feb 15, 2025
0c40dd7
Add more suggestions
donBarbos Feb 15, 2025
6e51ec3
Update docs
donBarbos Feb 15, 2025
4b85253
Update
donBarbos Feb 15, 2025
09d32b3
Update tests
donBarbos Feb 15, 2025
4b8786f
Correct style code
donBarbos Feb 15, 2025
96ba50d
Wrap the lines
donBarbos Feb 15, 2025
5d87f80
Wrap again
donBarbos Feb 15, 2025
05f5f65
Add seealso section
donBarbos Feb 15, 2025
e7a42f7
Update http.server.rst
donBarbos Feb 15, 2025
4c68c27
Merge branch 'main' into issue-85162
donBarbos Mar 16, 2025
3ca55d1
Update cli description
donBarbos Mar 16, 2025
3daf484
Update doc
donBarbos Mar 16, 2025
8b84be2
Update docs
donBarbos Apr 4, 2025
50e0ed5
Update Doc/whatsnew/3.14.rst
picnixz Apr 5, 2025
4f36fbf
Update Doc/whatsnew/3.14.rst
picnixz Apr 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update docs and replace password option
  • Loading branch information
donBarbos committed Feb 4, 2025
commit 96d4a686fe9fa2c993d66f0a4fbb68aba2285bc3
14 changes: 8 additions & 6 deletions Doc/library/http.server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ handler. Code to create and run the server looks like this::
specifying the Application-Layer Protocol Negotiation (ALPN) protocols
supported by the server. ALPN allows the server and client to negotiate
the application protocol during the TLS handshake. By default, it is set
to ``["http/1.1"]``, meaning the server will support HTTP/1.1. Other
possible values may include ``["h2", "http/1.1"]`` to enable HTTP/2
support.
to ``["http/1.1"]``, meaning the server will support HTTP/1.1.

.. versionadded:: next

Expand Down Expand Up @@ -499,14 +497,18 @@ following command runs an HTTP/1.1 conformant server::

The server can also support TLS encryption. The options ``--tls-cert`` and
``--tls-key`` allow specifying a TLS certificate chain and private key for
secure HTTPS connections. Use ``--tls-password`` option if private keys are
secure HTTPS connections. Use ``--tls-password-file`` option if private keys are
passphrase-protected. For example, the following command runs the server with
TLS enabled::

python -m http.server --tls-cert cert.pem --tls-key key.pem --tls-password
python -m http.server --tls-cert fullchain.pem

Or if a separate file with private key passphrase-protected::

python -m http.server --tls-cert cert.pem --tls-key key.pem --tls-password-file password.txt

.. versionchanged:: next
Added the ``--tls-cert``, ``--tls-key`` and ``--tls-password`` options.
Added the ``--tls-cert``, ``--tls-key`` and ``--tls-password-file`` options.

.. class:: CGIHTTPRequestHandler(request, client_address, server)

Expand Down
4 changes: 2 additions & 2 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ http
added to ``python -m http.server``:

* ``--tls-cert <path>``: Path to the TLS certificate file.
* ``--tls-key <path>``: Path to the private key file.
* ``--tls-password <password>``: Optional password for the private key.
* ``--tls-key <path>``: Optional path to the private key file.
* ``--tls-password-file <path>``: Optional path to the password for the private key.

(Contributed by Semyon Moroz in :gh:`85162`.)

Expand Down
45 changes: 21 additions & 24 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@
import time
import urllib.parse

try:
import ssl
except ImportError:
ssl = None

from getpass import getpass
from http import HTTPStatus


Expand Down Expand Up @@ -1262,9 +1256,12 @@ class HTTPSServer(HTTPServer):
def __init__(self, server_address, RequestHandlerClass,
bind_and_activate=True, *, certfile, keyfile=None,
password=None, alpn_protocols=None):
if ssl is None:
raise RuntimeError("SSL support missing")
try:
import ssl
except ImportError:
raise RuntimeError("SSL module is missing; HTTPS support is unavailable")

self.ssl = ssl
self.certfile = certfile
self.keyfile = keyfile
self.password = password
Expand All @@ -1281,10 +1278,8 @@ def server_activate(self):
self.socket = context.wrap_socket(self.socket, server_side=True)

def _create_context(self):
if ssl is None:
raise RuntimeError("SSL support missing")

context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
"""Create a secure SSL context."""
context = self.ssl.create_default_context(self.ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile=self.certfile,
keyfile=self.keyfile,
password=self.password)
Expand Down Expand Up @@ -1343,8 +1338,6 @@ def test(HandlerClass=BaseHTTPRequestHandler,
import argparse
import contextlib

PASSWORD_EMPTY = object()

parser = argparse.ArgumentParser()
parser.add_argument('--cgi', action='store_true',
help='run as CGI server')
Expand All @@ -1362,22 +1355,26 @@ def test(HandlerClass=BaseHTTPRequestHandler,
help='path to the TLS certificate')
parser.add_argument('--tls-key', metavar='PATH',
help='path to the TLS key')
parser.add_argument('--tls-password', metavar='PASSWORD', nargs='?',
default=None, const=PASSWORD_EMPTY,
help='password for the TLS key '
'(default: empty)')
parser.add_argument('--tls-password-file', metavar='PATH',
help='file containing the password for the TLS key')
parser.add_argument('port', default=8000, type=int, nargs='?',
help='bind to this port '
'(default: %(default)s)')
args = parser.parse_args()

if not args.tls_cert and args.tls_key:
parser.error('--tls-key requires --tls-cert to be set')
parser.error("--tls-key requires --tls-cert to be set")

tls_key_password = None
if args.tls_password_file:
if not args.tls_cert:
parser.error("--tls-password-file requires --tls-cert to be set")

if not args.tls_key and args.tls_password:
parser.error("--tls-password requires --tls-key to be set")
elif args.tls_password is PASSWORD_EMPTY:
args.tls_password = getpass("Enter the password for the TLS key: ")
try:
with open(args.tls_password_file, "r", encoding="utf-8") as f:
tls_key_password = f.read().strip()
except (OSError, IOError) as e:
parser.error(f"Failed to read TLS password file: {e}")

if args.cgi:
handler_class = CGIHTTPRequestHandler
Expand Down Expand Up @@ -1406,5 +1403,5 @@ def finish_request(self, request, client_address):
protocol=args.protocol,
tls_cert=args.tls_cert,
tls_key=args.tls_key,
tls_password=args.tls_password,
tls_password=tls_key_password,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The :mod:`http.server` module now includes built-in support for HTTPS
server. New :class:`http.server.HTTPSServer` class is an implementation of
HTTPS server that uses :mod:`ssl` module by providing a certificate and
private key. The ``--tls-cert``, ``--tls-key`` and ``--tls-password``
private key. The ``--tls-cert``, ``--tls-key`` and ``--tls-password-file``
arguments have been added to ``python -m http.server``. Patch by Semyon
Moroz.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy