0

Currently if I go to "http://localhost:8035/", I can see and have access to all the files, including root directory, client_files directory, and server_files directory(i.e. I have access to all files, folders, and all its subdirectories, etc).

Goal: I want to limit the file access to only the files in the client_files directory. Is there a way to do that with the current code I have?

Current Directory Structure:

enter image description here

Current Code (run_server.py - located in root directory):

from http.server import HTTPServer, SimpleHTTPRequestHandler

class CORSRequestHandler(SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET')
        self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
        return super(CORSRequestHandler, self).end_headers()

def func_run_server(url, port):
    httpd = HTTPServer((url, port), CORSRequestHandler)
    httpd.serve_forever()

func_run_server('localhost', 8035)

1 Answer 1

0

The SimpleHTTPRequestHandler serves the directory stack below the directory from which the server is running. The simplest solution is

import os
curD = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.join(curD, “client_files”))

before you start the server. Added bonus: you can run the script from anywhere and it won’t be sensitive to the directory from which it was launched.

Sign up to request clarification or add additional context in comments.

1 Comment

It works, but very hacky. Not quite the answer im looking for. Becomes problematic if for different folder structure types if the program gets bigger or certain files want to be restricted even in the clients_file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.