Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the recommended way of running a embedded web server within a desktop app (say wsgi server with pyqt)

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?

like image 780
btbytes Avatar asked Jun 08 '10 20:06

btbytes


People also ask

Why should I use WSGI?

WSGI servers are designed to handle many requests concurrently. Frameworks are not made to process thousands of requests and determine how to best route them from the server. WSGI speeds up Python web application development because you only need to know basic things about WSGI.

Why do we need WSGI for Python?

Nowadays, almost all Python frameworks use WSGI as a means, if not the only means, to communicate with their web servers. This is how Django, Flask, and many other popular frameworks do it.

Why do I need WSGI for flask?

Apache will use WSGI file to access our Flask application, so the WSGI file allows Apache to interact with Python as if it is native. It's a simple script.

How does WSGI server work?

WSGI stands for "Web Server Gateway Interface". It is used to forward requests from a web server (such as Apache or NGINX) to a backend Python web application or framework. From there, responses are then passed back to the webserver to reply to the requestor.


3 Answers

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.

like image 169
Ian Bicking Avatar answered Sep 20 '22 13:09

Ian Bicking


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

like image 25
zdav Avatar answered Sep 22 '22 13:09

zdav


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).

like image 28
lazy1 Avatar answered Sep 18 '22 13:09

lazy1