Thomas Thomas - 1 year ago 109
Bash Question

Simple http server in Python 3.5.1 Shell

I am trying to get a simple http server going, using the directory in my code below as the root. This is using Python 3.5.1 Shell:

>>> import os
>>> import http.server
>>> import socketserver
>>> os.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')
>>> python -m http.server 8000
SyntaxError: invalid syntax
>>> python -m SimpleHTTPServer 8000
SyntaxError: invalid syntax

I have looked at a similar topic: How to set up simple HTTP server in Python 3.5 on Windows 10? , but even when I try doing what the answer suggests, I still have the same problem ('invalid syntax').

Answer Source

You're confusing Python commands and shells command.

import os etc are python lines (interpreted by Python), python -m http.server 8000 is a shell line, interpreted by bash, sh or whatever Microsoft use for Windows. You may try something like this to run it in the Python REPL:

import os
from http.server import SimpleHTTPRequestHandler, HTTPServer                                                                                                                                   

server_address = ('', 8000)                                                                                                                                                                    
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)                                                                                                                                   

But the easiest way to do is probably to just run python -m http.server 8000 while being in the right directory in your terminal emulator.