简体   繁体   English

如何将 `directory` arg 传递给 Python 3 `http.server`?

[英]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 -我可以看到http.server需要一个目录参数 -

https://github.com/python/cpython/blob/master/Lib/http/server.py 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 -但我找不到任何可以接受该directory参数的语法 -

$ 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?我究竟做错了什么?

I suspect it's a problem with your Python version.我怀疑你的 Python 版本有问题。 Here's the same command on 3.7.4:这是 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 :根据文档directory参数仅在 3.7 版中添加:

New in version 3.7 : --directory specify alternate directory 3.7 版中的新功能:-- --directory指定备用目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM