Jason Collis Jason Collis - 1 year ago 134
Python Question

How to use SimpleHTTPServer?

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.

Answer Source

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 SimpleHTTPServer

and it should work as expected.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download