bottle.py will do this for you as a part of running the gevent server adapter.

https://github.com/defnull/bottle/blob/master/bottle.py#L36