Set the current directory when running a SimpleHTTPServer in python

Set the current directory when running a SimpleHTTPServer in python

To set the current directory when running a SimpleHTTPServer in Python, you can use the http.server module (Python 3.x) or the SimpleHTTPServer module (Python 2.x). Both modules allow you to specify the directory from which you want to serve files.

Here's how you can do it with Python 3.x's http.server module:

import http.server import socketserver # Specify the directory you want to serve files from directory = '/path/to/your/directory' # Change to the specified directory try: with open('/dev/null', 'w') as nullfile: with socketserver.TCPServer(("", 8000), http.server.SimpleHTTPRequestHandler) as httpd: httpd.socket = nullfile httpd.socket = httpd.listen(5) httpd.serve_forever() except KeyboardInterrupt: pass 

Replace '/path/to/your/directory' with the absolute path of the directory you want to serve files from. This code will start a basic HTTP server in the specified directory.

For Python 2.x, you can use the SimpleHTTPServer module like this:

import SimpleHTTPServer import SocketServer # Specify the directory you want to serve files from directory = '/path/to/your/directory' # Change to the specified directory try: os.chdir(directory) Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", 8000), Handler) httpd.serve_forever() except KeyboardInterrupt: pass 

Again, replace '/path/to/your/directory' with the absolute path of the directory you want to serve files from.

After running either of these scripts, you can access the files in the specified directory by opening a web browser and navigating to http://localhost:8000/.

Examples

    import os import http.server import socketserver os.chdir('/path/to/your/directory') # Change to desired directory port = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", port), Handler) as httpd: print("Serving at port", port) httpd.serve_forever() 
      import os import http.server import socketserver os.chdir('/path/to/specific/directory') port = 8080 with socketserver.TCPServer(("", port), http.server.SimpleHTTPRequestHandler) as server: print("Server running on port", port) server.serve_forever() 
        import os import http.server import socketserver os.chdir('/custom/directory') Handler = http.server.SimpleHTTPRequestHandler port = 8000 httpd = socketserver.TCPServer(("", port), Handler) print("Serving HTTP on port", port, "in directory:", os.getcwd()) httpd.serve_forever() 
          import os import http.server import socketserver os.chdir('/base/directory') Handler = http.server.SimpleHTTPRequestHandler port = 9000 with socketserver.TCPServer(("", port), Handler) as server: print("HTTP server running on port", port) server.serve_forever() 
            import os import http.server import socketserver os.chdir('/target/directory') port = 8000 httpd = socketserver.TCPServer(("", port), http.server.SimpleHTTPRequestHandler) print("Serving files from", os.getcwd()) httpd.serve_forever() 
              import os import http.server import socketserver os.chdir('/different/directory') Handler = http.server.SimpleHTTPRequestHandler port = 8080 httpd = socketserver.TCPServer(("", port), Handler) print("HTTP server serving from:", os.getcwd(), "on port", port) httpd.serve_forever() 
                import os import http.server import socketserver os.chdir('/serve/this/directory') Handler = http.server.SimpleHTTPRequestHandler port = 8000 with socketserver.TCPServer(("", port), Handler) as httpd: print("Serving from directory:", os.getcwd(), "on port", port) httpd.serve_forever() 
                  import os import http.server import socketserver os.chdir('/new/directory/path') port = 8000 httpd = socketserver.TCPServer(("", port), http.server.SimpleHTTPRequestHandler) print("HTTP server running in", os.getcwd(), "on port", port) httpd.serve_forever() 
                    import os import http.server import socketserver os.chdir('/custom/serving/directory') port = 8000 Handler = http.server.SimpleHTTPRequestHandler httpd = socketserver.TCPServer(("", port), Handler) print("Serving from:", os.getcwd()) httpd.serve_forever() 
                    1. "How to change HTTP server directory in Python?"

                    More Tags

                    twisted registry ionic-native logstash local-files exponentiation android-volley android-developer-api nameof stringify

                    More Python Questions

                    More Chemistry Calculators

                    More Dog Calculators

                    More Various Measurements Units Calculators

                    More Organic chemistry Calculators