How to use SimpleHTTPServer?

流过昼夜 提交于 2019-12-12 02:07:48

问题


I'm trying to start a simple http server with the most recent version of Python 2.7. I'm following a tutorial and it instructs me to do the following:

Open the Terminal then navigate to our client directory and enter the following command:

$ python -m SimpleHTTPServer

But no matter what I've tried, including other codes, it doesn't work. Can anyone help please? It always puts a syntax error such as:

'$' is not recognized as an internal or external command, operable program or batch file.

If I leave out the $, it returns:

'Python' is not recognized as an internal or external command, operable program or batch file.

I've tried starting python from the Python27 directory, then changing to the directory I want to start the server from and using the same commands, but nothing works! It then says Syntax Error.


回答1:


Firstly, if you're just starting to learn Python, I (and the members of the Stack Overflow Python community) strongly recommend using Python 3. Py2 is the past, Py3 is the present and future of the language. Support for Py2 ends in 2020, while Py3 will be supported indefinitely. You will learn some bad habits with Py2 that will make learning Py3 (which you'll have to do eventually) that much harder. Learn Py3 now, and when you're proficient with it you can then go back and see what the differences are with Py2. Also, stay away from the Learn Python the Hard Way tutorial. It's bad.

When you go to the download page I linked above, choose the installer for your version of Windows - if you're using 64-bit Windows, choose the 64-bit installer. When you run it, change the default installation directory to C:\Python35, and select the option to add the installation directory to your PATH. Once installation is complete, you can then uninstall Python 2 if you wish.

You can now open up the command line and run

python -m http.server

and it should work as expected.




回答2:


The dollar sign is not part of the command:

python -m SimpleHTTPServer

I guess it was simply used as a representation for the command line prompt in the tutorial you have been following



来源:https://stackoverflow.com/questions/39064577/how-to-use-simplehttpserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!