9

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.

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
  • 1
    The link contains code that is not legit, for python3. What you want is the SimpleHTTPRequestHandler and you try to use the BaseHTTPRequestHandler, so maybe it's a bogus article, and not that simple as the author claims. Tried and make it work but it didn't work for python 2 or 3 following the link. DIY! – Simon Oct 14 '16 at 02:06

1 Answers1

13

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)
Community
  • 1
  • 1
Avery
  • 2,270
  • 4
  • 33
  • 35