I've followed this link to build a simple file server with SSL.
from http.server import HTTPServer, BaseHTTPRequestHandler
import ssl
httpd = HTTPServer(('localhost', 4443), BaseHTTPRequestHandler)
# openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
httpd.socket = ssl.wrap_socket (httpd.socket, keyfile="key.pem", certfile='cert.pem', server_side=True)
httpd.serve_forever()
I have created a certificate successfully, key.pem
and cert.pem
file paths are cool and I can start the server using python server.py
. I am asked for a password, enter it, then it freezes for a bit and then it seems to run.
However, when I enter some URL such as https://localhost:4443/index.html
I get 500 Unsupported method GET. Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation. Do I need to do something more to make my server serve the current directory? Until now I have just used python -m http.server 8000
(SimpleHTTPServer
when on Mac.) I am using Python 3.
This is an will stay local so don't worry about the PEM
files and the server script being exposed through it (if it worked!). I am also okay with the certificate being untrusted and instructed Chrome to visit the page anyway. I just need it to allow me to access camera without having to deploy my app somewhere with a legit cert.
By default, SSL verification is enabled, and Requests will throw a SSLError if it’s unable to verify the certificate. Let us try to access a website with an invalid SSL certificate, using Python requests This website doesn’t have SSL setup so it raises this error.
To test for the presence of SSL support in a Python installation, user code should use the following idiom: This example creates a SSL context with the recommended security settings for client sockets, including automatic certificate verification:
If you want maximum compatibility between clients and servers, it is recommended to use PROTOCOL_TLS_CLIENT or PROTOCOL_TLS_SERVER as the protocol version. SSLv2 and SSLv3 are disabled by default.
The method unwrap () call does not return anything, unlike for an SSL socket where it returns the underlying socket. The server_name_callback callback passed to SSLContext.set_servername_callback () will get an SSLObject instance instead of a SSLSocket instance as its first parameter.
From the docs:
class http.server.BaseHTTPRequestHandler(request, client_address, server)
This class is used to handle the HTTP requests that arrive at the server. By itself, it cannot respond to any actual HTTP requests; it must be subclassed to handle each request method (e.g. GET or POST).
Try using SimpleHTTPRequestHandler instead, eg,
httpd = socketserver.TCPServer(('localhost', 4443), SimpleHTTPRequestHandler)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With