views:

27

answers:

2

The title may be a bit vague, but here's my goal: I have a frontend webserver which takes incoming HTTP requests, does some preprocessing on them, and then passes the requests off to my real webserver to get the HTTP response, which is then passed back to the client.

Currently, my frontend is built off of BaseHTTPServer.HTTPServer and the backend is CherryPy.

So the question is: Is there a way to take these HTTP requests / client connections and insert them into a CherryPy server to get the HTTP response? One obvious solution is to run an instance of the CherryPy backend on a local port or using UNIX domain sockets, and then the frontend webserver establishes a connection with the backend and relays any requests/responses. Obviously, this isn't ideal due to the overhead.

What I'd really like is for the CherryPy backend to not bind to any port, but just sit there waiting for the frontend to pass the client's socket (as well as the modified HTTP Request info), at which point it does its normal CherryPy magic and returns the request directly to the client.

I've been perusing the CherryPy source to find some way to accomplish this, and currently am attempting to modify wsgiserver.CherryPyWSGIServer, but it's getting pretty hairy and is probably not the best approach.

A: 

Is your main app a wsgi application? If so, you could write some middleware that wraps around it and does all the request wrangling before passing on to the main application.

If this this is possible it would avoid you having to run two webservers and all the problems you are encountering.

Ben
Thanks. That should do the trick. Initially I wanted to decouple the frontend and backend so I could replace CherryPy if necessary, but it looks like it's more trouble than it's worth. Plus, I guess using WSGI middleware, as long as the alternative "backend" is WSGI-compliant, replacing CherryPy with another webserver should be pretty easy.
Yep, I've moved apps from mod_wsgi and apache to uwsgi and nginx without touching the app.
Ben
A: 

Answered the Upgrade question at http://stackoverflow.com/questions/3859823/handling-http-1-1-upgrade-requests-in-cherrypy. Not sure if that addresses this one or not.

fumanchu
This question is more general, and is probably best solved using WSGI middleware. For really low-level socket handling (as in the linked question), the (linked) answer is definitely the best (only?) approach. Thanks.