How to run a http server which serves a specific path?

roipoussiere picture roipoussiere · Oct 1, 2016 · Viewed 73.9k times · Source

this is my Python3 project hiearchy:

projet
  \
  script.py
  web
    \
    index.html

From script.py, I would like to run a http server which serve the content of the web folder.

Here is suggested this code to run a simple http server:

import http.server
import socketserver

PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()

but this actually serve project, not web. How can I specify the path of the folder I want to serve?

Answer

Jerzy Pawlikowski picture Jerzy Pawlikowski · Apr 8, 2017

If you just want serve static file you can do it by running SimpleHTTPServer module using python 2:

 python -m SimpleHTTPServer

Or with python 3:

 python3 -m http.server

This way you do not need to write any script.