Securely Serve Static File Over HTTP Get Request In Python

73 Views Asked by At

I need to serve static files, like images and videos through URLs securely. That is, I need to password protect them. Is there any way I can prompt a password input when the user visits the URL or accept a password in any other way so that before loading the image, the browser asks for a password and gets a password from the session or something?

Basically, I am looking for a Python version to what is being done in the accepted answer here.

Using PHP/Apache to restrict access to static files (html, css, img, etc)

1

There are 1 best solutions below

0
cupCake On

I would consider a generic solution that doesn't belong to a specific programming language, in case I thought your needs are a basic need that can be accessed by only a webserver I'll use nginx to illustrate my answer tho.

Firstly, using apache2-utils utility package to create users and their password

$ sudo htpasswd -c /etc/apache2/.htpasswd UserName

then use auth_basic and auth_basic_user_file directives to achieve username:password authentication

server {
    listen 80; 
    listen [::]:80;

    server_name _;

    auth_basic           "Secure static content";
    auth_basic_user_file /etc/apache2/.htpasswd; 

    root /var/static;

    location ~*\.(jpg|jpeg|gif|pdf|png)$ {
        try_files $uri =404;
    }   

}

If you want to learn more about how to limit resource access with nginx, you can find additional information on the topic.