How can I create an local webserver for my python scripts?
Asked Answered
A

4

18

I'm looking to use a local webserver to run a series of python scripts for the user. For various unavoidable reasons, the python script must run locally, not on a server. As a result, I'll be using HTML+browser as the UI, which I'm comfortable with, for the front end.

I've been looking, therefore, for a lightweight web server that can execute python scripts, sitting in the background on a machine, ideally as a Windows service. Security and extensibility are not high priorities as it's all running internally on a small network.

Should I run a native python webserver as a Windows service (in which case, how)? Or is it just as easy to install Apache onto the user's machine and run as CGI? Since this is all local, performance is not an issue either.

Or am I missing something obvious?

Ardellearden answered 18/5, 2009 at 10:13 Comment(0)
A
17

Don't waste a lot of time creating Windows service.

Don't waste a lot of time on Windows Apache.

Just make a Python service that responds to HTTP requests.

Look at https://docs.python.org/2/library/basehttpserver.html
https://docs.python.org/3/library/http.server.html for version 3
Python offers an HTTP server that you can extend with your server-side methods.

Look at http://docs.python.org/library/wsgiref.html
Python offers a WSGI reference implementation that makes your server easy and standards-compliant.

Also http://fragments.turtlemeat.com/pythonwebserver.php


"I'm trying to avoid making the user run python stuff from the command prompt."

I don't see how clicking a web page is any different from clicking desktop icons.

Starting a web server based on Python is relatively easy, once you have the web server. First, build the server. Later, you can make sure the server starts. Let's look at some ways.

  1. Your user can't use a random browser to open your local page. They need a bookmark to launch "localhost:8000/myspecialserverinsteadofthedestop/" That bookmark can be a .BAT file that (1) runs the server, (2) runs firefox with the proper initial URL.

  2. You can put the server in the user's start-this menu.

  3. You can make your Python program a windows "service".

Are answered 18/5, 2009 at 10:19 Comment(2)
So I need to get the users to start up the server each time they want to use the framework? I'm trying to avoid making the user run python stuff from the command prompt. The turtlemeat link would need to be run manually unless it was a service.Ardellearden
+1 for wsgiref, excellent way to test your Python Web scripts locally on your machine.Explanatory
V
3

The wasiest step would be navigate to folder where your files are located and running http.server module

cd /yourapp
python3 -m http.server

the you should see something like this in console

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Virchow answered 22/9, 2022 at 15:45 Comment(0)
C
2

Best way is to make your own local server by using command prompt.

  1. Make a new folder say Project
  2. Make a new folder inside project & name it as "cgi-bin"(without quotes)
  3. Paste your .py file inside the cgi-bin folder
  4. Open cmd and change to the directory from which you want to run the server and type "python -m CGIHTTPServer"(without quotes)
  5. Minimize the cmd window & open your browser and type "localhost:8000/cgi-bin/yourpythonfilename.py"(without quotes).
Chainman answered 10/3, 2014 at 20:9 Comment(1)
C:\Program Files\Python39\python.exe: No module named CGIHTTPServerKrongold
F
-1

Running a native python webserver as a windows service should be a no brainer. Check out the documentation for writing windows services (win32api, ActiveState python) in python and also the documentation for subclassing BaseHttpServer and SimpleHttpServer.

BTW: I had a similar question on stackoverflow: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?

Basically, you subclass BaseHTTPServer (you have to anyway...) and then... but just read the accepted answer - it set me on the right track!

Flunky answered 18/5, 2009 at 10:20 Comment(1)
I have been through all the docs I can see on running a BaseHTTPServer as a service, but I couldn't see how to make it interruptable (so it would stop and start properly). IIS would mean a lot more learning and I was hoping to go with simple...Ardellearden

© 2022 - 2024 — McMap. All rights reserved.