<s> import argparse <EOL> import random <EOL> import os <EOL> import cherrypy <EOL> from ws4py . server . cherrypyserver import WebSocketPlugin , WebSocketTool <EOL> from ws4py . websocket import WebSocket <EOL> from ws4py . messaging import TextMessage <EOL> class ChatWebSocketHandler ( WebSocket ) : <EOL> def received_message ( self , m ) : <EOL> cherrypy . engine . publish ( '<STR_LIT>' , m ) <EOL> def closed ( self , code , reason = "<STR_LIT>" ) : <EOL> cherrypy . engine . publish ( '<STR_LIT>' , TextMessage ( reason ) ) <EOL> class Root ( object ) : <EOL> def __init__ ( self , host , port , ssl = False ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . scheme = '<STR_LIT>' if ssl else '<STR_LIT>' <EOL> @ cherrypy . expose <EOL> def index ( self ) : <EOL> return """<STR_LIT>""" % { '<STR_LIT:username>' : "<STR_LIT>" % random . randint ( <NUM_LIT:0> , <NUM_LIT:100> ) , '<STR_LIT:host>' : self . host , '<STR_LIT:port>' : self . port , '<STR_LIT>' : self . scheme } <EOL> @ cherrypy . expose <EOL> def ws ( self ) : <EOL> cherrypy . log ( "<STR_LIT>" % repr ( cherrypy . request . ws_handler ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import logging <EOL> from ws4py import configure_logger <EOL> configure_logger ( level = logging . DEBUG ) <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT:127.0.0.1>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = <NUM_LIT> , type = int ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> args = parser . parse_args ( ) <EOL> cherrypy . config . update ( { '<STR_LIT>' : args . host , <EOL> '<STR_LIT>' : args . port , <EOL> '<STR_LIT>' : os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) } ) <EOL> if args . ssl : <EOL> cherrypy . config . update ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> WebSocketPlugin ( cherrypy . engine ) . subscribe ( ) <EOL> cherrypy . tools . websocket = WebSocketTool ( ) <EOL> cherrypy . quickstart ( Root ( args . host , args . port , args . ssl ) , '<STR_LIT>' , config = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : ChatWebSocketHandler <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> ) </s>
