views:

82

answers:

4

The desktop app should start the web server on launch and should shut it down on close.

Assuming that the desktop is the only client allowed to connect to the web server, what is the best way to write this?

Both the web server and the desktop run in a blocking loop of their own. So, should I be using threads or multiprocessing?

+1  A: 

Have a look at the BaseHTTPServer package, or better yet the SimpleHTTPServer. Pretty simple and easy to use.

zdav
A: 

My company makes a commercial product designed for this purpose called the Neokernel Web Server; you can limit the server so it only responds to connections from specific network interfaces. There is a demo project that shows starting / controlling the server from within your own application.

It runs ASP.NET, WCF, static pages, and allows you to write custom server extensions in C# or VB.NET; a download and a free 30 day evaluation license is available from http://www.neokernel.com. Note that the Neokernel Web Server does NOT support python out of the box.

Damien
A: 

In Sauce RC, we use CherryPy. Since it's pure Python, it's very easy to embed it (as source on disk or in a zip file).

lazy1
+3  A: 

Use something like CherryPy or paste.httpserver. You can use wsgiref's server, and it generally works okay locally, but if you are doing Ajax the single-threaded nature of wsgiref can cause some odd results, or if you ever do a subrequest you'll get a race condition. But for most cases it'll be fine. It might be useful to you not to have an embedded threaded server (both CherryPy and paste.httpserver are threaded), in which case wsgiref would be helpful (all requests will run from the same thread).

Note that if you use CherryPy or paste.httpserver all requests will automatically happen in subthreads (those packages do the thread spawning for you), and you probably will not be able to directly touch the GUI code from your web code (since GUI code usually doesn't like to be handled by threads). For any of them the server code blocks, so you need to spawn a thread to start the server in. Twisted can run in your normal GUI event loop, but unless that's important it adds a lot of complexity.

Do not use BaseHTTPServer or SimpleHTTPServer, they are silly and complicated and in all cases where you might use then you should use wsgiref instead. Every single case, as wsgiref is has a sane API (WSGI) while these servers have silly APIs.

Ian Bicking
This script (http://bitbucket.org/julian/hatta-mirror/src/tip/hatta_gtkicon.py) comes very close to what I was looking for. It uses wsgiref too.
btbytes