Justin
Justin

Reputation: 4853

How to pass `directory` arg to Python 3 `http.server`?

So this works fine -

$ python3 -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

And I can see that http.server takes a directory argument -

https://github.com/python/cpython/blob/master/Lib/http/server.py

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    {...}
    parser.add_argument('--directory', '-d', default=os.getcwd(),
                        help='Specify alternative directory '
                        '[default:current directory]')
    {...}           

But I can't find any kind of syntax that will accept that directory argument -

$ python3 -m http.server -d site 8000
usage: server.py [-h] [--cgi] [--bind ADDRESS] [port]
server.py: error: argument port: invalid int value: 'site'
$ python3 -m http.server 8000 -d site
usage: server.py [-h] [--cgi] [--bind ADDRESS] [port]
server.py: error: unrecognized arguments: -d site
$ python3 -m http.server 8000 --directory site
usage: server.py [-h] [--cgi] [--bind ADDRESS] [port]
server.py: error: unrecognized arguments: --directory site

What am I doing wrong ?

Upvotes: 2

Views: 2876

Answers (1)

Pavel Vergeev
Pavel Vergeev

Reputation: 3390

I suspect it's a problem with your Python version. Here's the same command on 3.7.4:

▶ python3 -m http.server -h
usage: server.py [-h] [--cgi] [--bind ADDRESS] [--directory DIRECTORY] [port]

positional arguments:
  port                  Specify alternate port [default: 8000]

optional arguments:
  -h, --help            show this help message and exit
  --cgi                 Run as CGI Server
  --bind ADDRESS, -b ADDRESS
                        Specify alternate bind address [default: all
                        interfaces]
  --directory DIRECTORY, -d DIRECTORY
                        Specify alternative directory [default:current
                        directory]

~                                                                                                                                   
▶ python3 --version
Python 3.7.4

The directory argument is only added in version 3.7 according to the docs:

New in version 3.7: --directory specify alternate directory

Upvotes: 7

Related Questions