<s> import sys <EOL> import os <EOL> import pkg_resources <EOL> import StringIO <EOL> from sys import argv , executable <EOL> from socket import AF_INET <EOL> startupMsgs = [ ] <EOL> if '<STR_LIT>' in sys . platform : <EOL> from twisted . internet import kqreactor <EOL> kqreactor . install ( ) <EOL> startupMsgs . append ( "<STR_LIT>" ) <EOL> elif sys . platform . startswith ( '<STR_LIT>' ) : <EOL> from twisted . internet import epollreactor <EOL> epollreactor . install ( ) <EOL> startupMsgs . append ( "<STR_LIT>" ) <EOL> elif sys . platform . startswith ( '<STR_LIT>' ) : <EOL> from twisted . internet import kqreactor <EOL> kqreactor . install ( ) <EOL> startupMsgs . append ( "<STR_LIT>" ) <EOL> elif sys . platform == '<STR_LIT:win32>' : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> from twisted . internet import reactor <EOL> startupMsgs . append ( "<STR_LIT>" % ( str ( reactor . __class__ ) , pkg_resources . require ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . version ) ) <EOL> from twisted . internet import reactor <EOL> from twisted . python import log <EOL> from twisted . internet import reactor <EOL> from twisted . internet . protocol import Factory <EOL> from twisted . web . server import Site <EOL> from twisted . web . static import File <EOL> from autobahn . websocket . util import parse_url <EOL> from autobahn . twisted . websocket import WebSocketServerFactory , WebSocketServerProtocol <EOL> from autobahn . util import Stopwatch <EOL> hasStatprof = False <EOL> try : <EOL> import statprof <EOL> startupMsgs . append ( "<STR_LIT>" ) <EOL> hasStatprof = True <EOL> except ImportError : <EOL> startupMsgs . append ( "<STR_LIT>" ) <EOL> class Stats : <EOL> def __init__ ( self ) : <EOL> self . period = <NUM_LIT:0> <EOL> self . clients = <NUM_LIT:0> <EOL> self . tMsgs = <NUM_LIT:0> <EOL> self . tOctets = <NUM_LIT:0> <EOL> self . tHandshakes = <NUM_LIT:0> <EOL> self . tOctetsWireIn = <NUM_LIT:0> <EOL> self . tOctetsWireOut = <NUM_LIT:0> <EOL> self . stopwatch = Stopwatch ( start = False ) <EOL> self . _advance ( ) <EOL> def _advance ( self ) : <EOL> self . period += <NUM_LIT:1> <EOL> self . pMsgs = <NUM_LIT:0> <EOL> self . pOctets = <NUM_LIT:0> <EOL> self . pHandshakes = <NUM_LIT:0> <EOL> self . pOctetsWireIn = <NUM_LIT:0> <EOL> self . pOctetsWireOut = <NUM_LIT:0> <EOL> self . stopwatch . resume ( ) <EOL> def trackHandshake ( self ) : <EOL> self . tHandshakes += <NUM_LIT:1> <EOL> self . pHandshakes += <NUM_LIT:1> <EOL> def trackMsg ( self , length ) : <EOL> self . tMsgs += <NUM_LIT:1> <EOL> self . pMsgs += <NUM_LIT:1> <EOL> self . tOctets += length <EOL> self . pOctets += length <EOL> def trackOctetsWireIn ( self , count ) : <EOL> self . tOctetsWireIn += count <EOL> self . pOctetsWireIn += count <EOL> def trackOctetsWireOut ( self , count ) : <EOL> self . tOctetsWireOut += count <EOL> self . pOctetsWireOut += count <EOL> def stats ( self , advance = True ) : <EOL> elapsed = self . stopwatch . stop ( ) <EOL> s = ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT:\n>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT:\n>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" <EOL> ) % ( self . period , <EOL> round ( elapsed , <NUM_LIT:3> ) , <EOL> self . clients , <EOL> self . pHandshakes , <EOL> round ( float ( self . pHandshakes ) / elapsed ) , <EOL> self . pMsgs , <EOL> round ( float ( self . pMsgs ) / elapsed ) , <EOL> self . pOctets , <EOL> round ( float ( self . pOctets ) / elapsed ) , <EOL> self . pOctetsWireIn , <EOL> round ( float ( self . pOctetsWireIn ) / elapsed ) , <EOL> self . pOctetsWireOut , <EOL> round ( float ( self . pOctetsWireOut ) / elapsed ) , <EOL> self . tHandshakes , <EOL> self . tMsgs , <EOL> self . tOctets , <EOL> self . tOctetsWireIn , <EOL> self . tOctetsWireOut , <EOL> ) <EOL> self . _advance ( ) <EOL> return s <EOL> class EchoServerProtocol ( WebSocketServerProtocol ) : <EOL> def onOpen ( self ) : <EOL> self . factory . stats . clients += <NUM_LIT:1> <EOL> self . factory . stats . trackHandshake ( ) <EOL> def onMessage ( self , msg , binary ) : <EOL> self . sendMessage ( msg , binary ) <EOL> self . factory . stats . trackMsg ( len ( msg ) ) <EOL> def onClose ( self , wasClean , code , reason ) : <EOL> self . factory . stats . clients -= <NUM_LIT:1> <EOL> def connectionLost ( self , reason ) : <EOL> WebSocketServerProtocol . connectionLost ( self , reason ) <EOL> self . factory . stats . trackOctetsWireIn ( self . trafficStats . preopenIncomingOctetsWireLevel + <EOL> self . trafficStats . incomingOctetsWireLevel ) <EOL> self . factory . stats . trackOctetsWireOut ( self . trafficStats . preopenOutgoingOctetsWireLevel + <EOL> self . trafficStats . outgoingOctetsWireLevel ) <EOL> class EchoServerFactory ( WebSocketServerFactory ) : <EOL> protocol = EchoServerProtocol <EOL> def __init__ ( self , wsuri ) : <EOL> WebSocketServerFactory . __init__ ( self , wsuri ) <EOL> self . stats = Stats ( ) <EOL> def master ( options ) : <EOL> """<STR_LIT>""" <EOL> if not options . silence : <EOL> print "<STR_LIT>" % os . getpid ( ) <EOL> if options . port : <EOL> webdir = File ( "<STR_LIT:.>" ) <EOL> web = Site ( webdir ) <EOL> web . log = lambda _ : None <EOL> reactor . listenTCP ( options . port , web ) <EOL> factory = Factory ( ) <EOL> port = reactor . listenTCP ( options . wsport , factory , backlog = options . backlog ) <EOL> port . stopReading ( ) <EOL> for i in range ( options . workers ) : <EOL> args = [ executable , "<STR_LIT>" , __file__ , "<STR_LIT>" , str ( port . fileno ( ) ) , "<STR_LIT>" , str ( i ) ] <EOL> args . extend ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> reactor . spawnProcess ( <EOL> None , executable , args , <EOL> childFDs = { <NUM_LIT:0> : <NUM_LIT:0> , <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , port . fileno ( ) : port . fileno ( ) } , <EOL> env = os . environ ) <EOL> reactor . run ( ) <EOL> PROFILER_FREQ = <NUM_LIT> <EOL> def worker ( options ) : <EOL> """<STR_LIT>""" <EOL> workerPid = os . getpid ( ) <EOL> if not options . noaffinity : <EOL> p = psutil . Process ( workerPid ) <EOL> print "<STR_LIT>" , p . cpu_affinity ( ) <EOL> p . cpu_affinity ( [ options . cpuid ] ) <EOL> print "<STR_LIT>" , p . cpu_affinity ( ) <EOL> factory = EchoServerFactory ( options . wsuri ) <EOL> reactor . adoptStreamPort ( options . fd , AF_INET , factory ) <EOL> if not options . silence : <EOL> print "<STR_LIT>" % ( workerPid , factory , factory . protocol ) <EOL> if options . profile : <EOL> statprof . reset ( PROFILER_FREQ ) <EOL> statprof . start ( ) <EOL> if not options . silence : <EOL> def stat ( ) : <EOL> if options . profile : <EOL> statprof . stop ( ) <EOL> output = StringIO . StringIO ( ) <EOL> output . write ( "<STR_LIT:->" * <NUM_LIT> + "<STR_LIT:\n>" ) <EOL> output . write ( "<STR_LIT>" % ( workerPid , factory . stats . stats ( ) ) ) <EOL> if options . profile : <EOL> output . write ( "<STR_LIT:\n>" ) <EOL> statprof . display ( output ) <EOL> output . write ( "<STR_LIT:->" * <NUM_LIT> + "<STR_LIT>" ) <EOL> sys . stdout . write ( output . getvalue ( ) ) <EOL> if options . profile : <EOL> statprof . reset ( PROFILER_FREQ ) <EOL> statprof . start ( ) <EOL> reactor . callLater ( options . interval , stat ) <EOL> reactor . callLater ( options . interval , stat ) <EOL> if False : <EOL> import cProfile <EOL> print "<STR_LIT>" <EOL> cProfile . run ( '<STR_LIT>' ) <EOL> else : <EOL> reactor . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import argparse <EOL> import psutil <EOL> DEFAULT_WORKERS = psutil . cpu_count ( ) <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = str , default = u'<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT:port>' , type = int , default = <NUM_LIT> , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = int , default = DEFAULT_WORKERS , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = int , default = <NUM_LIT> , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = int , default = <NUM_LIT:5> , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = int , default = None , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = int , default = None , help = '<STR_LIT>' ) <EOL> options = parser . parse_args ( ) <EOL> if options . profile and not hasStatprof : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> isSecure , host , wsport , resource , path , params = parse_url ( options . wsuri ) <EOL> options . wsport = wsport <EOL> if options . fd is not None : <EOL> worker ( options ) <EOL> else : <EOL> if not options . silence : <EOL> for m in startupMsgs : <EOL> print m <EOL> master ( options ) </s>
