Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SSL server gives me "501 Unsupported method GET"

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.

like image 715
Tomáš Hübelbauer Avatar asked Oct 13 '16 22:10

Tomáš Hübelbauer


People also ask

Why is my SSL certificate not working in Python requests?

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.

How do I test for SSL support in a Python installation?

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:

Which SSL protocol version should I use?

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.

What does the method unwrap () do in SSL?

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.


1 Answers

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)
like image 76
Avery Avatar answered Oct 22 '22 19:10

Avery