Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active July 13, 2024 18:43
Show Gist options
  • Save masakielastic/134a0a4578e97cbdc258c2b5c532935f to your computer and use it in GitHub Desktop.
Save masakielastic/134a0a4578e97cbdc258c2b5c532935f to your computer and use it in GitHub Desktop.
CGIHTTPRequestHandler の使い方 (Python 3.13 で非推奨、Python 3.15 で削除)
from http.server import HTTPServer, CGIHTTPRequestHandler
import ssl
def run(host, port, ctx, handler):
server = HTTPServer((host, port), handler)
server.socket = ctx.wrap_socket(server.socket)
print('Server Starts - %s:%s' % (host, port))
try:
server.serve_forever()
except KeyboardInterrupt:
pass
server.server_close()
print('Server Stops - %s:%s' % (host, port))
if __name__ == '__main__':
host = 'localhost'
port = 8000
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ctx.load_cert_chain('server.crt', keyfile='server.key')
ctx.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1
handler = CGIHTTPRequestHandler
handler.cgi_directories = ['/cgi-bin', '/htbin']
# https://stackoverflow.com/a/27303995/531320
handler.have_fork=False
run(host, port, ctx, handler)
node.cgi
#!/usr/bin/env node
console.log("Content-type: text/plain; charset=utf-8\n");
console.log("Hello World");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment