Actively writing an HTTPS webserver in Python 3
When importing names from a location (module, package, dynamic library, ...), don't mix multiple locations on a single line. That is, this:

import os, ssl


... should be:

import os
import ssl


This is purely a stylistic concern, and it's one of the rules listed in PEP-8, the official Python style guide.
Global variables should be a last resort. They clutter the global namespace, are hard to track when reading the code, and are very prone to race conditions when using threads. Here, you could pass the context as an argument, or perhaps set up another class with the context attached as an attribute.

Let's say you have to share foo between func_a() and func_b(). Three different approaches:


Make foo global (analogous to what you did with the context, not great):

foo = Foo()

def func_a():
    foo.method_1()

def func_b():
    foo.method_2()

func_a()
func_b()

Pass foo as an argument:

def func_a(foo):
    foo.method_1()

def func_b(foo):
    foo.method_2()

def main():
    foo = Foo()
    func_a(foo=foo)
    func_b(foo=foo)

Make foo a class attribute:

class MyClass:
    def __init__(self, foo):
        self.foo = foo

    def func_a(self):
        self.foo.method_1()

    def func_b(self):
        self.foo.method_2()

my_class = MyClass(Foo())
my_class.func_a()
my_class.func_b()



Even if you stick to using global variables, setting global sslcontext is unnecessary because you're not reassigning it. If you're not assigning to a variable, Python will:

i. Try to find the name locally;

ii. If that fails, look for the name in global scope;

iii. Finally, raise a NameError to indicate the variable doesn't exist in any scope.

Therefore, if you reference sslcontext locally, the interpreter will find the global name (ii.). But it can't read your mind. If you instead assign sslcontext = 42, Python will assume you want to create a fresh, local variable with the name 'sslcontext' and the value of 42, rather than reassigning the global sslcontext. That's it. That's where global comes in, and that's all it's good for.
In HSTSHandler.end_headers(), this idiom:

SimpleHTTPRequestHandler.end_headers(self)


... could be more clearly expressed by calling super(), then super().end_headers(). This is good practice, and also assures multi-inheritance is handled correctly.
Source code encoding does not need to be set explicitly here, as ASCII will suffice.


That's as much as I can give you without more context to the question.