1

I have a simple python script that I want to serve as a website:

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

I'm in that folder and run

$ python3 -m http.server 

then I visit http://hassbian.local:8000/song.py

The terminal says this and I get the file as a txt file, the script won't execute.

Serving HTTP on 0.0.0.0 port 8000 ...
192.168.1.115 - - [04/Jun/2017 14:19:59] "GET / HTTP/1.1" 200 -
192.168.1.115 - - [04/Jun/2017 14:20:04] "GET /song.py HTTP/1.1" 200 -

Running on a rasberry pi

7
  • 2
    You cannot execute scripts like this. python3 -m http.server will simply serve files from the directory on which you ran it. Commented Jun 4, 2017 at 12:27
  • what should I use instead? Commented Jun 4, 2017 at 12:30
  • It doesn't really have to be python, any simple scripting language is fine by me. Commented Jun 4, 2017 at 12:31
  • Probably simplest to do a basic flask application. Commented Jun 4, 2017 at 12:32
  • What do you want to do? You want to build a website or something else? Commented Jun 4, 2017 at 12:36

1 Answer 1

1

SimpleHTTPServer doesn't do CGI. If you want CGI you will have to use CGIHTTPServer

This module can run CGI scripts on Unix and Windows systems.

I am not sure if that's what you really want, but invoking a python script in the manner you have shown is CGI. CGI is a really old way of doing things. Running simple web apps with python is now almost exclusively the domain of webapp2 or flask. While more complex apps involving databases are dominated by django.

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

3 Comments

great info, cgi seems nice since it's already running on my rasberry pi, I'm creating a small webservice that will serve a couple of files in a folder as a json-object, would cgi be enough?
Well if it's just json, SimpleHTTPServer is fine. Your question showed you trying to run a python file.
Well, it's a dynamically created json file, based on the contents of a folder.

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.