4

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?

277roshan
  • 354
  • 1
  • 3
  • 13

4 Answers4

2

Use simple HTTP server:

  python -m http.server 8000

Or for Python 2:

  python -m SimpleHTTPServer 8000

https://docs.python.org/3/library/http.server.html

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
0

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.
Cloud Cho
  • 1,594
  • 19
  • 22
0
  1. keep the files in a some folder which you want to access from a localhost

  2. In command prompt go to that location and type python -m http.server 8080

  3. now type localhost :8080 in browser you will able to access files in that folder

  4. 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

desertnaut
  • 57,590
  • 26
  • 140
  • 166
0

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.

ccpizza
  • 28,968
  • 18
  • 162
  • 169