NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

modfastcgi.py (2950B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\test\modfastcgi.py
      2 import os
      3 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
      4 import re
      5 import sys
      6 import time
      7 import cherrypy
      8 from cherrypy.process import plugins, servers
      9 from cherrypy.test import helper
     10 
     11 def read_process(cmd, args = ''):
     12     pipein, pipeout = os.popen4('%s %s' % (cmd, args))
     13     try:
     14         firstline = pipeout.readline()
     15         if re.search('(not recognized|No such file|not found)', firstline, re.IGNORECASE):
     16             raise IOError('%s must be on your system path.' % cmd)
     17         output = firstline + pipeout.read()
     18     finally:
     19         pipeout.close()
     20 
     21     return output
     22 
     23 
     24 APACHE_PATH = 'apache2ctl'
     25 CONF_PATH = 'fastcgi.conf'
     26 conf_fastcgi = '\n# Apache2 server conf file for testing CherryPy with mod_fastcgi.\n# fumanchu: I had to hard-code paths due to crazy Debian layouts :(\nServerRoot /usr/lib/apache2\nUser #1000\nErrorLog %(root)s/mod_fastcgi.error.log\n\nDocumentRoot "%(root)s"\nServerName 127.0.0.1\nListen %(port)s\nLoadModule fastcgi_module modules/mod_fastcgi.so\nLoadModule rewrite_module modules/mod_rewrite.so\n\nOptions +ExecCGI\nSetHandler fastcgi-script\nRewriteEngine On\nRewriteRule ^(.*)$ /fastcgi.pyc [L]\nFastCgiExternalServer "%(server)s" -host 127.0.0.1:4000\n'
     27 
     28 def erase_script_name(environ, start_response):
     29     environ['SCRIPT_NAME'] = ''
     30     return cherrypy.tree(environ, start_response)
     31 
     32 
     33 class ModFCGISupervisor(helper.LocalWSGISupervisor):
     34     httpserver_class = 'cherrypy.process.servers.FlupFCGIServer'
     35     using_apache = True
     36     using_wsgi = True
     37     template = conf_fastcgi
     38 
     39     def __str__(self):
     40         return 'FCGI Server on %s:%s' % (self.host, self.port)
     41 
     42     def start(self, modulename):
     43         cherrypy.server.httpserver = servers.FlupFCGIServer(application=erase_script_name, bindAddress=('127.0.0.1', 4000))
     44         cherrypy.server.httpserver.bind_addr = ('127.0.0.1', 4000)
     45         cherrypy.server.socket_port = 4000
     46         self.start_apache()
     47         cherrypy.engine.start()
     48         self.sync_apps()
     49 
     50     def start_apache(self):
     51         fcgiconf = CONF_PATH
     52         if not os.path.isabs(fcgiconf):
     53             fcgiconf = os.path.join(curdir, fcgiconf)
     54         f = open(fcgiconf, 'wb')
     55         try:
     56             server = repr(os.path.join(curdir, 'fastcgi.pyc'))[1:-1]
     57             output = self.template % {'port': self.port,
     58              'root': curdir,
     59              'server': server}
     60             output = output.replace('\r\n', '\n')
     61             f.write(output)
     62         finally:
     63             f.close()
     64 
     65         result = read_process(APACHE_PATH, '-k start -f %s' % fcgiconf)
     66         if result:
     67             print result
     68 
     69     def stop(self):
     70         read_process(APACHE_PATH, '-k stop')
     71         helper.LocalWSGISupervisor.stop(self)
     72 
     73     def sync_apps(self):
     74         cherrypy.server.httpserver.fcgiserver.application = self.get_app(erase_script_name)