Created
November 3, 2025 20:11
-
-
Save corysolovewicz/1fb8589a9014e3bf587f77827c6edd23 to your computer and use it in GitHub Desktop.
Python HTTP file server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #You can run save and run this python code in your VM then connect to it from your host using a web browser. | |
| import http.server | |
| import socketserver | |
| import os | |
| # Configuration | |
| PORT = 8000 | |
| DIRECTORY = "files" # Change this to the folder you want to serve | |
| class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler): | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, directory=DIRECTORY, **kwargs) | |
| if __name__ == "__main__": | |
| # Ensure the directory exists | |
| if not os.path.isdir(DIRECTORY): | |
| os.makedirs(DIRECTORY) | |
| with socketserver.TCPServer(("", PORT), MyHttpRequestHandler) as httpd: | |
| print(f"Serving '{DIRECTORY}' directory at http://localhost:{PORT}") | |
| print("Press Ctrl+C to stop the server.") | |
| httpd.serve_forever() | |
| #Save this script as `file_server.py`. | |
| #Create a directory named files (or change `DIRECTORY` to your preferred folder). | |
| #Place the files you want to share inside that directory. | |
| #Run: | |
| #python3 file_server.py | |
| #Visit `http://<vm-ip-address>:8000` in your browser. You’ll see a directory listing, and you can click files to download them. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment