I want to access files in my local machine by using urls. For example "file:///usr/local/home/thapaliya/constants.py". What would be the best way to achieve this?
4 Answers
Use simple HTTP server:
python -m http.server 8000
Or for Python 2:
python -m SimpleHTTPServer 8000

- 82,057
- 50
- 264
- 435
I copied from orszacky's answer for the similar question:
A little http server locally.
For Windows, the easiest is to install http-server globally using node's package manager:
npm install -g http-server
Then simply run http-server in any of your project directories:
C:\my_project> http-server
You can also use Python in Windows, and follow the instruction below:
For Linux, since Python is usually available in most Linux distributions,
run python -m SimpleHTTPServer
in your project directory, and you can load your page on http://localhost:8000
In Python 3 the SimpleHTTPServer module has been merged into http.server, so the new command is
python3 -m http.server.

- 1,594
- 19
- 22
keep the files in a some folder which you want to access from a localhost
In command prompt go to that location and type python -m http.server 8080
now type localhost :8080 in browser you will able to access files in that folder
if u want to use some js files for paticular html files then
<script src="http://localhost:8080/main.js"</script>
make sure you run this program on another port

- 57,590
- 26
- 140
- 166

- 9
- 3
Had the same need and have been searching for something simple but more versatile than python's built-in HTTP server (which is meant for debugging only and can't do range requests, i.e. doesn't handle streaming video properly).
Ended up writing a simple Web UI wrapper for caddy2 which relies on caddy's ability to return directory listings as JSON so the entire logic is browser-side. The main intent is to support audio/video playing in one frame while browsing related HTML/PDF or text in another split frame (there is some simple mapping with regexes for matching multimedia files with the related text files).
The old and currently unmaintained version is using python's built-in HTTP server which was extended to support range requests.

- 28,968
- 18
- 162
- 169