4

I have spent hours banging my head against the wall on this one!

I am running Python 2.7.10 on a Mac. I have a few Python scripts that I have written which run inside the Atom editor.

I have been trying to run these scripts on a local web server to speed up development. I have tried MAMP (which just throws 500 Internal Server Errors), and now "python -m SimpleHTTPServer" which just displays the python code in the browser and doesn't seem to execute it.

I have chmod +x my .py files. I start the web server in the folder that contains the .py files.

Here is an example...

hello.py

print("hello world")

When I browse to http://localhost:8000/hello.py I get the raw code displayed in the browser.

print("hello world")

If I use terminal and enter "python hello.py" it runs and displays the correct output...

MacBook-Pro-3:folder dj$ python hello.py

hello world

I have tried dozens of tutorials and suggested solutions, but none seem to help. Am I missing something fundamental here?

Thanks!

D

1
  • SimpleHTTPServer can't do that, try CGIHTTPServer instead which can execute scripts if placed in the right directory. A better approach would be to go back to an Apache server and configure it properly (this isn't a topic for Stack Overflow). Commented Dec 14, 2018 at 2:46

1 Answer 1

4

The server that python -m SimpleHTTPServer (or, for future readers, python -m http.server on Python 3) spins will not execute any file. It is merely a server that serves files as the documentation suggests:

The SimpleHTTPServer module can be used in the following manner in order to set up a very basic web server serving files relative to the current directory.

In order to get a server that will actually execute Python code, you'll need to use another tool. I'd start with bottle or flask.

Sign up to request clarification or add additional context in comments.

1 Comment

Had a feeling I was missing something quite fundamental! Many thanks, I now have a "hello world"! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.