Thomas Thomas - 6 months ago 42
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

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                                                                                                                                   

os.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')                                                                                                                                                                                      
server_address = ('', 8000)                                                                                                                                                                    
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)                                                                                                                                   
httpd.serve_forever()

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.

Comments