<s> import gevent <EOL> from gevent . pywsgi import WSGIServer , WSGIHandler <EOL> import logging <EOL> import os <EOL> import socket <EOL> import threading <EOL> import traceback <EOL> from ajenti . api import * <EOL> from ajenti . plugins import manager <EOL> from ajenti . util import public <EOL> @ public <EOL> @ rootcontext <EOL> @ interface <EOL> class IPCHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_name ( self ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , args ) : <EOL> """<STR_LIT>""" <EOL> class IPCWSGIHandler ( WSGIHandler ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> WSGIHandler . __init__ ( self , * args , ** kwargs ) <EOL> self . client_address = ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def log_request ( self ) : <EOL> pass <EOL> class IPCSocketServer ( WSGIServer ) : <EOL> pass <EOL> def ipc_application ( environment , start_response ) : <EOL> name , args = environment [ '<STR_LIT>' ] . split ( '<STR_LIT:/>' ) <EOL> args = args . decode ( '<STR_LIT>' ) . splitlines ( ) <EOL> logging . info ( '<STR_LIT>' % ( name , args ) ) <EOL> for h in IPCHandler . get_all ( manager . context ) : <EOL> if h . get_name ( ) == name : <EOL> try : <EOL> result = h . handle ( args ) <EOL> if result is None : <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> return '<STR_LIT>' <EOL> else : <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> return result <EOL> except Exception as e : <EOL> traceback . print_exc ( ) <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> return str ( e ) <EOL> break <EOL> else : <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> @ public <EOL> @ plugin <EOL> @ persistent <EOL> @ rootcontext <EOL> class IPCServer ( BasePlugin ) : <EOL> def start ( self ) : <EOL> gevent . spawn ( self . run ) <EOL> def run ( self ) : <EOL> socket_path = '<STR_LIT>' <EOL> sock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> if os . path . exists ( socket_path ) : <EOL> os . unlink ( socket_path ) <EOL> sock . bind ( socket_path ) <EOL> sock . listen ( <NUM_LIT:5> ) <EOL> os . chmod ( socket_path , <NUM_LIT:0> <NUM_LIT> ) <EOL> server = IPCSocketServer ( sock , application = ipc_application , handler_class = IPCWSGIHandler ) <EOL> server . serve_forever ( ) </s>
