| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import cherrypy |
|---|
| 4 | import os |
|---|
| 5 | from os.path import * |
|---|
| 6 | import sys, urllib |
|---|
| 7 | |
|---|
| 8 | import conary.lib.util |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | from cherrypy._cpwsgi import wsgiApp |
|---|
| 12 | |
|---|
| 13 | from paste.util.scgiserver import SWAP |
|---|
| 14 | from scgi import scgi_server |
|---|
| 15 | from types import MethodType |
|---|
| 16 | import signal |
|---|
| 17 | import socket |
|---|
| 18 | import time |
|---|
| 19 | import errno |
|---|
| 20 | |
|---|
| 21 | class Root(object): |
|---|
| 22 | @cherrypy.expose() |
|---|
| 23 | def index(self): |
|---|
| 24 | time.sleep(60) |
|---|
| 25 | return "Hello World" |
|---|
| 26 | |
|---|
| 27 | cherrypy.config.update({'autoreload.on': False}) |
|---|
| 28 | |
|---|
| 29 | class NewSCGIServer(scgi_server.SCGIServer): |
|---|
| 30 | def serve_on_socket(self, s): |
|---|
| 31 | self.socket = s |
|---|
| 32 | self.socket.listen(40) |
|---|
| 33 | signal.signal(signal.SIGHUP, self.hup_signal) |
|---|
| 34 | while 1: |
|---|
| 35 | try: |
|---|
| 36 | conn, addr = self.socket.accept() |
|---|
| 37 | for x in xrange(3,1024): |
|---|
| 38 | try: |
|---|
| 39 | os.close(x) |
|---|
| 40 | except: |
|---|
| 41 | pass |
|---|
| 42 | except socket.error, e: |
|---|
| 43 | if e[0] != errno.EINTR: |
|---|
| 44 | raise # something weird |
|---|
| 45 | if self.restart: |
|---|
| 46 | self.do_restart() |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | def serve_application (application, prefix, port): |
|---|
| 50 | class rAASCGIAppHandler(SWAP): |
|---|
| 51 | def __init__(self, *args, **kwargs): |
|---|
| 52 | self.prefix = prefix |
|---|
| 53 | self.app_obj = application |
|---|
| 54 | SWAP.__init__(self, *args, **kwargs) |
|---|
| 55 | |
|---|
| 56 | server = NewSCGIServer(rAASCGIAppHandler, port=port) |
|---|
| 57 | |
|---|
| 58 | server.serve() |
|---|
| 59 | |
|---|
| 60 | cherrypy.root = Root() |
|---|
| 61 | |
|---|
| 62 | # Start the server |
|---|
| 63 | cherrypy.server.start(init_only=True, server_class=None) |
|---|
| 64 | |
|---|
| 65 | serve_application(application=wsgiApp, prefix="/", port=4000) |
|---|