Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script not running with local web server, just displays code in browser

Tags:

python

apache

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

like image 869
Deej Avatar asked Sep 13 '25 03:09

Deej


1 Answers

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.

like image 60
DeepSpace Avatar answered Sep 15 '25 16:09

DeepSpace