}


class ThreadMixin(object):
    executor = ThreadPoolExecutor(max_workers=4)


class FallbackHandler(ThreadMixin, web.FallbackHandler):
    @run_on_executor
    def prepare(self):
        self.fallback(self.request)
        self._finished = True


application = web.Application([
    url(r'/media/(.*)', web.StaticFileHandler, {'path': settings.MEDIA_ROOT}),
    url(r'.*', FallbackHandler, {'fallback': wsgi.WSGIContainer(django_app)})
], **APP_SETTINGS)


def main():
    if APP_SETTINGS['debug']:
        sys.stdout.write('Host: {}\n'.format(options.host))
        sys.stdout.write('Port: {}\n\n'.format(options.port))
    application.listen(options.port, options.host)
    try:
        IOLoop.instance().start()
    except KeyboardInterrupt:
        IOLoop.instance().stop()


if __name__ == '__main__':
class PeopleIdHandler(web.RequestHandler):
    def initialize(self, name):
        self.name = name

    def get(self, id, *args, **kwargs):
        # self.write("用户id:{}".format(id))
        self.redirect(self.reverse_url("people_name", "bobby"))


class PeopleNameHandler(web.RequestHandler):
    def get(self, name, *args, **kwargs):
        self.write("用户姓名:{}".format(name))


class PeopleInfoHanler(web.RequestHandler):
    def get(self, name, age, gender, *args, **kwargs):
        self.write("用户姓名:{}, 用户年龄:{}, 用户性别:{}".format(name, age, gender))


urls = [
    web.URLSpec(r'/', MainHandler, name="index"),
    web.URLSpec(r'/people/(?P<id>\d+)/?', PeopleIdHandler, {"name": "wanlaji"}, name="people_id"),
    web.URLSpec(r'/people/(?P<name>\w+)/?', PeopleNameHandler, name="people_name"),
    web.URLSpec(r'/people/(\w+)/(\d+)/(\w+)/?', PeopleInfoHanler, name="people_info"),
]

if __name__ == "__main__":
    app = web.Application(urls, debug=True)
    app.listen(8888)
    ioloop.IOLoop.current().start()
Beispiel #3
0
def create_preload_application():
    app = web.Application([(r"/preload", MyHandler)])
    server = app.listen(12345, address="127.0.0.1")
    tornado.ioloop.IOLoop.instance().start()
Beispiel #4
0
        self.router = router

    def post(self):
        obj = json.loads(self.request.body)

        for client in clients:
            if client.is_closed:
                clients.remove(client)
            else:
                client.send(self.request.body)
        print "[{}] {}".format(OnBarsHandler.i, self.request.body) 
        OnBarsHandler.i = OnBarsHandler.i+1

    def put(self):
        # TODO(max): sanity checks, security checks
        for client in clients:
            if client.is_closed:
                clients.remove(client)
            else:
                client.send(self.request.body)

if __name__ == '__main__':
    ClientRouter = SockJSRouter(ClientConnection, '/echo')

    app = web.Application(
        [(r"/", IndexHandler)] +
        [(r"/onbars", OnBarsHandler, dict(router=ClientRouter))] +
        ClientRouter.urls)
    app.listen(9999)
    ioloop.IOLoop.instance().start()
Beispiel #5
0
                            f"INSERT INTO msg(name, email, address, msg) "
                            f"VALUES('{name}', '{email}', '{address}', '{msg}')"
                        )
                    else:
                        await cur.execute(
                            f"UPDATE msg SET "
                            f"name='{name}', email='{email}', address='{address}', msg='{msg}' WHERE id='{sid}'"
                        )
                await conn.commit()


settings = {
    "static_path": "/home/zpf/workarea/realForum/MsgBoard/static",
    "static_prefix": "/static/",
    "template_path": "templates",
    "db": {
        'host': '127.0.0.1',
        'user': '******',
        'password': '******',
        'po   rt': 3306,
        'db': 'rfm'
    }
}

if __name__ == "__main__":
    app = web.Application([
        web.URLSpec("/", IndexHandler, {"db": settings["db"]}, name="index"),
    ], debug=True, **settings)
    app.listen(8080)
    ioloop.IOLoop.current().start()
Beispiel #6
0
 def run(self):
     application = web.Application(self.__handlers, **self.__settings)
     logger.info("Server started at {}:{}", self.address, self.port)
     application.listen(self.port, address=self.address)
     io_loop = ioloop.IOLoop.instance()
     io_loop.start()
Beispiel #7
0
Datei: main.py Projekt: 0-1-0/v3
        cid = self.get_argument('id')
        if cid:
            method_name = self.request.uri.split('/')[2] + '_service'
            method = getattr(self._nc, method_name)
            yield gen.Task(method, cid)

        self.redirect('/')


class TLocatorServer(TTornado.TTornadoServer):
    def __init__(self):
        handler = ServiceLocator()
        processor = TServiceLocator.Processor(handler)
        pfactory = TBinaryProtocol.TBinaryProtocolFactory()
        super(TLocatorServer, self).__init__(processor, pfactory)


application = web.Application(handlers=[
    (r"/", MainHandler),
    (r"/service/.*", ServiceHandler),
],
                              template_path=os.path.join(APP_ROOT, 'views'))

if __name__ == "__main__":
    application.listen(HTTP_PORT)
    tserver = TLocatorServer()
    tserver.bind(THRIFT_PORT)
    tserver.start(1)

    ioloop.IOLoop.instance().start()
Beispiel #8
0
    try:
        port = int(args[1])
    except IndexError:
        port = 8000

    conn = sqlite3.connect(dbpath)
    conn.enable_load_extension(True)
    conn.load_extension("mod_spatialite.so")

    application = web.Application([
        (r'/view/(?P<tag>.*)', MapViewHandler),
        (r'/summits/(?P<prefix>[0-9a-f]*)', SummitsHandler, {
            'mode': 'up'
        }),
        (r'/sinks/(?P<prefix>[0-9a-f]*)', SummitsHandler, {
            'mode': 'down'
        }),
        (r'/(.*)', web.StaticFileHandler, {
            'path': 'static'
        }),
    ],
                                  template_path='templates',
                                  debug=True)
    application.listen(port)

    try:
        IOLoop.instance().start()
    except KeyboardInterrupt:
        pass
    except Exception, e:
        print e
        raise
Beispiel #9
0
        logger.info(self.request.body.decode("utf-8"))
        res = yield gen.Task(bert_align_tasks_enzh.alignment.apply_async,
                             args=[self.request.body])
        ret = res.result
        self.write(ret)
        logger.info(ret)
        self.finish()


if __name__ == '__main__':
    log_format = "%(asctime)s-%(levelname)s-%(message)s"
    os.makedirs("align_bert_enzh_logs", exist_ok=True)
    handler = TimedRotatingFileHandler(
        'align_bert_enzh_logs/alignment_enzh_bert.log',
        backupCount=30,
        when='midnight',
        interval=1,
        encoding="utf-8")
    formatter = logging.Formatter(log_format)
    handler.setFormatter(formatter)

    handler.suffix = "%Y%m%d"

    logger = logging.getLogger("alignment_enzh_bert")
    logger.setLevel(logging.INFO)
    logger.addHandler(handler)

    application = web.Application([(r"/align_bert", AsyncAppBertHandler)])
    application.listen(port=args.port, address=args.host)
    ioloop.IOLoop.instance().start()
Beispiel #10
0

"""

import os
import sys
import tornado
from tornado.options import options
import pymongo

sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__))))
from tornado import ioloop, web
# Handling the configuration
import config
from handler_list import handler_list

app = web.Application(handler_list,
                      static_path=options.static_path,
                      autoreload=True,
                      cookie_secret=options.cookie_secret)

if __name__ == '__main__':
    print('$appname$ is running on http://localhost:{}/'.format(
        options.app_port))
    app.listen(options.app_port, xheaders=True)
    ioloop = ioloop.IOLoop.instance()
    tornado.autoreload.start()
    tornado.autoreload.watch('web/tester.html')

    ioloop.start()
class PeopleUserCenter(web.RequestHandler):
    def initialize(self, name):  # **kwargs 中的参数会映射进来
        self.db_name = name

    async def get(self, age, name, *args, **kwargs):
        print(self.db_name)
        self.write("Usercenter of {}:Age:{}".format(name, age))


people_db = {"name": "people"}

urls = [
    (r'/', MainHandler),
    (r'/people/(\d+)/?', PeopleIdHandler),
    # 不指定对应参数,后面的问号可以在链接没有'/'的时候补全
    (r'/people/(\w+)/(\d+)/(\w+)/?', PeopleInfoHandler),
    # 直接通过 web 中的 URLSpec 实例化,好处是可以传入 name,在 redirect 的时候比较方便
    tornado.web.URLSpec(r'/people/(?P<name>\w+)/center/(?P<age>\d+)/?',
                        PeopleUserCenter,
                        people_db,
                        name="usercenter"),
]

settings = {"template_path": "templates", "debug": True}

if __name__ == "__main__":
    app = web.Application(urls, **settings)
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
Beispiel #12
0
            if found_one_term == False:
                    self.write('exception: term not found in document')
                    print ('could not find term ' + str(doc_id) + ' ' + str(term))
                    self.write(MyText)
                    print (MyText)
                    print (len(result))
            RS = [MyDict]
            self.write(json.JSONEncoder().encode({"results": RS}))            ### print as a dictionary
        else:
            ### Raise exception
            self.write('exception: document not found')            ### print as a dictionary
            print ('could not find ' + str(doc_id))
            ###  ##
            ##print 'well, \033[1m' + 'Hello \033[0m there' 
            ####
            

#The output should be JSON-encoded
    

if __name__ == "__main__":

    task_id = process.fork_processes(inventory.num_doc)
    
    D_BASE_PORT = inventory.port2 
    port = D_BASE_PORT + task_id 

    app = httpserver.HTTPServer(web.Application([web.url(r"/doc", DocHandler, dict(port=port))]))
    app.add_sockets(netutil.bind_sockets(port))
    print ('docServer' + str(task_id) + ' at port: ' + str(port))
    tornado.ioloop.IOLoop.current().start()
Beispiel #13
0
from apps.hotels.urls import url_patterns as hotels_urls
from apps.reports.urls import url_patterns as reports_urls
from apps.admin.urls import url_patterns as admin_urls
from settings import settings
import ui_methods

define("port", default=8000, help="run on the given port", type=int)
define("debug", default=True, help="debug mode", type=bool)

if __name__ == "__main__":
    print("Run server")
    parse_command_line()

    # add url patterns
    handlers = []
    handlers.extend(main_urls)
    handlers.extend(user_urls)
    handlers.extend(hotels_urls)
    handlers.extend(reports_urls)
    handlers.extend(admin_urls)

    # create tornado web app
    app = web.Application(handlers=handlers,
                          ui_methods=ui_methods,
                          autoreload=True,
                          debug=options.debug,
                          **settings)
    http_server = httpserver.HTTPServer(app)
    http_server.listen(options.port)
    ioloop.IOLoop.instance().start()
Beispiel #14
0

class ImagesUploadRequestHandler(web.RequestHandler):
    """ test for multiple file upload """
    def post(self):
        global received_files

        received_files = self.request.files['images']


""" global variables """

pet_db = create_pet_db()
app = web.Application(
    [(r'/api/pet', PetRequestHandler, dict(db=pet_db)),
     (r'/api/pet/(\d+)', PetIdRequestHandler, dict(db=pet_db)),
     (r'/api/pet/uploadImage', ImageRequestHandler),
     (r'/upload', ImagesUploadRequestHandler)],
    debug=True)


class TornadoTestCase(testing.AsyncHTTPTestCase):
    """
    """
    def setUp(self):
        global received_file
        global received_meta

        # reset global
        received_file = received_meta = None

        super(TornadoTestCase, self).setUp()
Beispiel #15
0
 def init_tornado_application(self):
     """Instantiate the tornado Application object"""
     self.tornado_application = web.Application(self.handlers,
                                                **self.tornado_settings)
Beispiel #16
0
from tornado.ioloop import IOLoop
from tornado import gen, web


class ExampleHandler(web.RequestHandler):
    @gen.coroutine
    def get(self):
        delay = self.get_argument('delay', 5)
        yield gen.sleep(int(delay))
        self.write({"status": 1, "msg": "success"})
        self.finish()

#   @gen.coroutine()
#   def post(self):
#      pass

application = web.Application(
    [
        (r"/example", ExampleHandler),
        #(r"/other", OtherHandler),
    ],
    autoreload=True)
application.listen(8765)
IOLoop.current().start()
        if not storage.websockets:
            storage.websockets = set()
        storage.websockets.add(self)

    def on_message(self, message):
        pass

    def on_close(self):
        log("WebSocket closed")
        storage.websockets.remove(self)


lamp_client = web.Application([
    (r'/ws', SocketHandler),
    (r'/(.*)', web.StaticFileHandler, {
        'path': os.path.join(os.path.dirname(__file__), "static"),
        'default_filename': 'index.html'
    }),
])


@gen.coroutine
def tcp_client_coroutine():
    """Setup the connection to the echo server and wait for user
    input.
    """
    stream = None
    try:
        stream = yield tcp_client.connect(
            os.environ.get('LAMP_ADDRESS') or '127.0.0.1',
            int(os.environ.get('LAMP_PORT') or 9999))
Beispiel #18
0
def start():
    app = web.Application([("/", MainHandler)])
    app.listen(8080)
    ioloop.IOLoop.current().start()
Beispiel #19
0
def main():
    # command-line options
    define("debug", default=False, help="run in debug mode", type=bool)
    define("no_cache", default=False, help="Do not cache results", type=bool)
    define("localfiles", default="", help="Allow to serve local files under /localfile/* this can be a security risk", type=str)
    define("port", default=5000, help="run on the given port", type=int)
    define("cache_expiry_min", default=10*60, help="minimum cache expiry (seconds)", type=int)
    define("cache_expiry_max", default=2*60*60, help="maximum cache expiry (seconds)", type=int)
    define("mc_threads", default=1, help="number of threads to use for Async Memcache", type=int)
    define("threads", default=1, help="number of threads to use for rendering", type=int)
    define("processes", default=0, help="use processes instead of threads for rendering", type=int)
    define("frontpage", default=FRONTPAGE_JSON, help="path to json file containing frontpage content", type=str)
    define("sslcert", help="path to ssl .crt file", type=str)
    define("sslkey", help="path to ssl .key file", type=str)
    tornado.options.parse_command_line()

    # NBConvert config
    config = Config()
    config.HTMLExporter.template_file = 'basic'
    config.NbconvertApp.fileext = 'html'
    config.CSSHTMLHeaderTransformer.enabled = False
    # don't strip the files prefix - we use it for redirects
    # config.Exporter.filters = {'strip_files_prefix': lambda s: s}

    # DEBUG env implies both autoreload and log-level
    if os.environ.get("DEBUG"):
        options.debug = True
        logging.getLogger().setLevel(logging.DEBUG)

    # setup memcache
    mc_pool = ThreadPoolExecutor(options.mc_threads)
    if options.processes:
        # can't pickle exporter instances,
        exporter = HTMLExporter
        pool = ProcessPoolExecutor(options.processes)
    else:
        exporter = HTMLExporter(config=config, log=log.app_log)
        pool = ThreadPoolExecutor(options.threads)

    memcache_urls = os.environ.get('MEMCACHIER_SERVERS',
        os.environ.get('MEMCACHE_SERVERS')
    )

    # Handle linked Docker containers
    if(os.environ.get('NBCACHE_PORT')):
        tcp_memcache = os.environ.get('NBCACHE_PORT')
        memcache_urls = tcp_memcache.split('tcp://')[1]

    if(os.environ.get('NBINDEX_PORT')):
        log.app_log.info("Indexing notebooks")
        tcp_index = os.environ.get('NBINDEX_PORT')
        index_url = tcp_index.split('tcp://')[1]
        index_host, index_port = index_url.split(":")
        indexer = ElasticSearch(index_host, index_port)
    else:
        log.app_log.info("Not indexing notebooks")
        indexer = NoSearch()

    if options.no_cache:
        log.app_log.info("Not using cache")
        cache = MockCache()
    elif pylibmc and memcache_urls:
        kwargs = dict(pool=mc_pool)
        username = os.environ.get('MEMCACHIER_USERNAME', '')
        password = os.environ.get('MEMCACHIER_PASSWORD', '')
        if username and password:
            kwargs['binary'] = True
            kwargs['username'] = username
            kwargs['password'] = password
            log.app_log.info("Using SASL memcache")
        else:
            log.app_log.info("Using plain memecache")

        cache = AsyncMultipartMemcache(memcache_urls.split(','), **kwargs)
    else:
        log.app_log.info("Using in-memory cache")
        cache = DummyAsyncCache()

    # setup tornado handlers and settings

    template_path = pjoin(here, 'templates')
    static_path = pjoin(here, 'static')
    env = Environment(loader=FileSystemLoader(template_path))
    env.filters['markdown'] = markdown.markdown
    try:
        git_data = git_info(here)
    except Exception as e:
        app_log.error("Failed to get git info: %s", e)
        git_data = {}
    else:
        git_data['msg'] = escape(git_data['msg'])


    if options.no_cache:
        # force jinja to recompile template every time
        env.globals.update(cache_size=0)
    env.globals.update(nrhead=nrhead, nrfoot=nrfoot, git_data=git_data,
        ipython_info=ipython_info(), len=len,
    )
    AsyncHTTPClient.configure(HTTPClientClass)
    client = AsyncHTTPClient()
    github_client = AsyncGitHubClient(client)

    # load frontpage sections
    with io.open(options.frontpage, 'r') as f:
        frontpage_sections = json.load(f)

    # cache frontpage links for the maximum allowed time
    max_cache_uris = {''}
    for section in frontpage_sections:
        for link in section['links']:
            max_cache_uris.add('/' + link['target'])

    settings = dict(
        log_function=log_request,
        jinja2_env=env,
        static_path=static_path,
        client=client,
        github_client=github_client,
        exporter=exporter,
        config=config,
        index=indexer,
        cache=cache,
        cache_expiry_min=options.cache_expiry_min,
        cache_expiry_max=options.cache_expiry_max,
        max_cache_uris=max_cache_uris,
        frontpage_sections=frontpage_sections,
        pool=pool,
        gzip=True,
        render_timeout=20,
        localfile_path=os.path.abspath(options.localfiles),
        fetch_kwargs=dict(
            connect_timeout=10,
        ),
    )

    # create and start the app
    if options.localfiles:
        log.app_log.warning("Serving local notebooks in %s, this can be a security risk", options.localfiles)
        # use absolute or relative paths:
        handlers.insert(0, (r'/localfile/(.*)', LocalFileHandler))

    # load ssl options
    ssl_options = None
    if options.sslcert:
        ssl_options = {
            'certfile' : options.sslcert,
            'keyfile' : options.sslkey,
        }

    app = web.Application(handlers, debug=options.debug, **settings)
    http_server = httpserver.HTTPServer(app, xheaders=True, ssl_options=ssl_options)
    log.app_log.info("Listening on port %i", options.port)
    http_server.listen(options.port)
    ioloop.IOLoop.instance().start()
Beispiel #20
0
    app = web.Application(
        [
            (r'/', MainHandler),
            (r'/test', TestHandler),
            (r'/news', NewsHandler),
            (r'/signup', SignupHandler),
            (r'/deauth', UserDeauth),
            (r'/leave', UserUnauth),
            (r'/policy', PolicyHandler),
            (r'/about', AboutHandler),
            (r'/auth/google', GoogleAuth),
            (r'/auth/facebook', FacebookAuth),
            (r'/auth/spotify', SpotifyAuth),
            (r'/auth/twitter', TwitterAuth),
            (r'/auth/reddit', RedditAuth),
            (r'/auth/tumblr', TumblrAuth),
            (r'/auth/instagram', InstagramAuth),
            (r'/auth/close', CloseWindow),

            (r'/user/info', UserAuth),

            (r'/api/showtimes', ListShowtimesHandler),
            (r'/api/showtimes/access_tokens', ShowtimeAccessTokens),
            (r'/api/showtimes/create', CreateShowtimeHandler),
            (r"/favicon.ico", web.StaticFileHandler, {"path": ""}),
        ],
        template_path="./templates/",
        static_path="./static/",
        debug=debug,
        cookie_secret=CONFIG.get('cookie_secret'),
        base_url=CONFIG.get('base_url'),
        **oauth_creds
    )
        imgs = searchSoup.select("img ")
        for m in imgs:
            print(m.get('src'))

        elements = searchSoup.select('meta[name="viewport"]')
        if len(elements) > 0:
            return "true"

        # 2 .富门户移动化过的标志
        fmhmark = searchSoup.find_all(
            src=re.compile("http://gmwz-1251053291.file.myqcloud.com/"))

        if len(fmhmark) > 0:
            return "true"

        return "false"

    ##截取网页移动端浏览快照
    def get_html_image(self, chrome):
        return chrome.get_screenshot_as_base64()


#启动服务  监听mobile_check路径
application = server.Application([
    (r"/mobile_check", MainHandler),
])

if __name__ == "__main__":
    application.listen(18114)
    loop.IOLoop.instance().start()
Beispiel #22
0
def make_app():
    return web.Application([
        (r"/login", LoginHandler),
        (r"/files(/.+)?", FileHandler),
    ])
Beispiel #23
0
    'db': 'testdb'
}


class GetUserHandler(web.RequestHandler):
    POOL = pools.Pool(
        connParam,
        max_idle_connections=1,
        max_recycle_sec=3,
    )

    @gen.coroutine
    def get(self):
        userid = self.get_argument('id')
        cursor = yield self.POOL.execute(
            """select name from user where id = {}""".format(userid))
        if cursor.rowcount > 0:
            self.write({"status": 1, "name": cursor.fetchone()[0]})
        else:
            self.write({"status": 0, "name": ""})
        self.finish()


if __name__ == '__main__':
    application = web.Application([
        (r"/getuser", GetUserHandler),
    ],
                                  autoreload=True)
    application.listen(8765)
    IOLoop.current().start()
      super(RequestHandler, self).__init__(*args, **kwargs)

   def prepare(self):
      uniq_id = self.request.headers.get('X-UniqID', uuid.uuid4().hex)
      self.logger = logging.LoggerAdapter(
         self.parent_log.getChild('RequestHandler'),
         extra={'context': uniq_id})

   def get(self, object_id):
      self.logger.debug('fetchin %s', object_id)
      self.set_status(200)
      return self.finish()

def sig_handler(signo, frame):
   logging.info('caught signal %d, stopping IO loop', signo)
   iol = ioloop.IOLoop.instance()
   iol.add_callback_from_signal(iol.stop)

if __name__ == '__main__':
   logging.config.dictConfig(LOG_CONFIG)
   logger = logging.getLogger('app')
   app = web.Application([
      web.url('/(?P<object_id>\w+)', RequestHandler,
              kwargs={'parent_log': logger}),
   ])
   app.listen(8000)
   signal.signal(signal.SIGINT, sig_handler)
   signal.signal(signal.SIGTERM, sig_handler)
   ioloop.IOLoop.instance().start()
   logger.info('IO loop stopped, exiting')
Beispiel #25
0
def Main():
    """Entrant function."""
    parser = optparse.OptionParser()
    parser.add_option('-c',
                      '--cluster',
                      metavar='CLUSTER_MANAGER',
                      action='store',
                      type='string',
                      dest='cluster',
                      help='specify cluster manager',
                      default='GoogleCompute')
    parser.add_option('-p',
                      '--http-port',
                      metavar='PORT_NUM',
                      action='store',
                      type='string',
                      dest='http_port',
                      help='port to start http server on',
                      default='12345')
    parser.add_option('-P',
                      '--sim-port',
                      metavar='SIMULATOR_PORT_NUM',
                      action='store',
                      type='string',
                      dest='bigbrain_port',
                      help='port that bigbrain instances run on',
                      default='32123')
    parser.add_option('-b',
                      '--heartbeat',
                      metavar='SECONDS',
                      action='store',
                      type='float',
                      dest='heartbeat_timeout',
                      help='duration between cluster manager heartbeats',
                      default='3.0')
    (options, args) = parser.parse_args()  # pylint: disable-msg=W0612

    # Spin up database
    dbconn = pymongo.connection.Connection()
    database = dbconn.bigbrain

    # Initialize Cluster Manager
    cluster_type = {
        'Localhost': lcm.LocalhostClusterManager,
        'GoogleCompute': gccm.GoogleComputeClusterManager
    }

    if options.cluster not in cluster_type:
        print 'Unable to instantiate cluster manager of type "%s"' % (
            options.cluster)
        sys.exit(2)

    print 'Initializing ' + options.cluster
    cluster_manager = cluster_type[options.cluster]()

    cluster_manager.Initialize(socket.gethostname(), options.http_port,
                               options.bigbrain_port)
    cluster_manager.Heartbeat()

    # Initialize RPC engine
    rpc_manager = bigbrain.rpc.AsyncSimulatorRPC(cluster_manager,
                                                 options.bigbrain_port,
                                                 'msgpack', True)

    # Start up resource controller
    resource_controller = bigbrain.resourcecontroller.ResourceController(
        cluster_manager, rpc_manager, database)

    # Set up web handlers
    handler_params = dict(db=database,
                          cluster=cluster_manager,
                          rpc=rpc_manager,
                          rc=resource_controller)

    url_handlers = [(r'/agent/(msgpack|json)/(.*)', AgentUpdate,
                     handler_params),
                    (r'/viewstream', viewstream.ViewStream, handler_params),
                    (r'/experiments', MockExperiments, handler_params),
                    (r'/experiments/(.*)', MockExperiments, handler_params),
                    (r'/social/?(.*)', MockSocial, handler_params),
                    (r'/static/(.*)', web.StaticFileHandler, {
                        'path': './static'
                    }), (r'/login', Login, handler_params),
                    (r'/logout', Logout, handler_params),
                    (r'/(.*)', MockHome, handler_params)]

    application = web.Application(url_handlers,
                                  template_path='./templates/',
                                  cookie_secret='batsbatsbatsEVERYWHEREB@Tbot',
                                  debug=True)
    application.listen(options.http_port)

    def InstanceHeartbeatCallback():
        InstanceHeartbeat(cluster_manager, rpc_manager, resource_controller)
        ioloop.IOLoop.instance().add_timeout(
            time.time() + options.heartbeat_timeout, InstanceHeartbeatCallback)

    ioloop.IOLoop.instance().add_timeout(
        time.time() + options.heartbeat_timeout, InstanceHeartbeatCallback)
    ioloop.IOLoop.instance().start()
    logging.debug('Server shutdown')
Beispiel #26
0
        def consume():
            # Start a tornado IOLoop for reading/writing to the process in this thread
            ioloop.IOLoop()
            reader = streams.JsonRpcStreamReader(proc.stdout)
            reader.listen(lambda msg: self.write_message(json.dumps(msg)))

        thread = threading.Thread(target=consume)
        thread.daemon = True
        thread.start()

    def on_message(self, message):
        """Forward client->server messages to the endpoint."""
        print(message)
        self.writer.write(json.loads(message))

    def check_origin(self, origin):
        return True


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--host", type=str, default="127.0.0.1")
    parser.add_argument("--port", type=int, default=3000)
    args = parser.parse_args()
    app = web.Application([
        (r"/cpp", LanguageServerWebSocketHandler),
    ])
    print("Started Web Socket at ws://{}:{}/cpp".format(args.host, args.port))
    app.listen(args.port, address=args.host)
    ioloop.IOLoop.current().start()
Beispiel #27
0
 def _start_server(cls):
     cls.io_loop = ioloop.IOLoop.current()
     app = web.Application([(r".*", TestingApp)])
     cls.server, cls.port = run_tornado_app(app, cls.io_loop, cls.certs,
                                            cls.scheme, cls.host)
     cls.server_thread = run_loop_in_thread(cls.io_loop)
Beispiel #28
0
            'digrams': digrams,
            'trigrams': trigrams,
            'check_links': check_links,
            'article': {
                'title': article.title,
                'desc': article.meta_description,
                'text': article.cleaned_text,
                'top_image': img,
            }
        }
        self.write_message(json.dumps(result))
        self.close()


app = web.Application(
    [
        (r'/', IndexHandler),
        (r'/ws', SocketHandler),
        #    (r'/api', ApiHandler),
        #    (r'/(favicon.ico)', web.StaticFileHandler, {'path': '../'}),
        #    (r'/(rest_api_example.png)', web.StaticFileHandler, {'path': './'}),
    ],
    autoreload=True,
    debug=True,
    compiled_template_cache=False,
    serve_traceback=True)

if __name__ == '__main__':
    app.listen(8888)
    ioloop.IOLoop.instance().start()
Beispiel #29
0
def create_worker_preload_application():
    application = web.Application([(r"/preload", WorkerPreloadHandler)])
    server = application.listen(12346, address="127.0.0.1")
    tornado.ioloop.IOLoop.instance().start()
Beispiel #30
0
def commandline(argv):
    from . import bgtasks

    version_string, git_hash = get_version_info()
    logger.info('starting up Librarian %s (%s)', version_string, git_hash)
    app.config['_version_string'] = version_string
    app.config['_git_hash'] = git_hash

    server = app.config.get('server', 'flask')
    host = app.config.get('host', None)
    port = app.config.get('port', 21106)
    debug = app.config.get('flask_debug', False)
    n_server_processes = app.config.get('n_server_processes', 1)

    if host is None:
        print(
            'note: no "host" set in configuration; server will not be remotely accessible',
            file=sys.stderr)

    maybe_add_stores()

    if n_server_processes > 1:
        if server != 'tornado':
            print('error: can only use multiple processes with Tornado server',
                  file=sys.stderr)
            sys.exit(1)

    if server == 'tornado':
        # Need to set up HTTP server and fork subprocesses before doing
        # anything with the IOLoop.
        from tornado.wsgi import WSGIContainer
        from tornado.httpserver import HTTPServer
        from tornado.ioloop import IOLoop
        from tornado import web
        from .webutil import StreamFile

        flask_app = WSGIContainer(app)
        tornado_app = web.Application([
            (r'/stream/.*', StreamFile),
            (r'.*', web.FallbackHandler, {
                'fallback': flask_app
            }),
        ])

        http_server = HTTPServer(tornado_app)
        http_server.bind(port, address=host)
        http_server.start(n_server_processes)
        db.engine.dispose()  # force new connection after potentially forking

    do_mandc = app.config.get('report_to_mandc', False)
    if do_mandc:
        from . import mc_integration
        mc_integration.register_callbacks(version_string, git_hash)

    if server == 'tornado':
        # Set up periodic report on background task status; also reminds us
        # that the server is alive.
        bgtasks.register_background_task_reporter()

        if is_primary_server():
            # Primary server is also in charge of checking out whether there's
            # anything to do with our standing orders.
            from tornado.ioloop import IOLoop
            from . import search
            IOLoop.current().add_callback(search.queue_standing_order_copies)
            search.register_standing_order_checkin()

        # Hack the logger to indicate which server we are.
        import tornado.process
        taskid = tornado.process.task_id()
        if taskid is not None:
            fmtr = logging.getLogger('').handlers[0].formatter
            fmtr._fmt = fmtr._fmt.replace(': ', ' #%d: ' % taskid)

    if server == 'flask':
        print(
            'note: using "flask" server, so background operations will not work',
            file=sys.stderr)
        app.run(host=host, port=port, debug=debug)
    elif server == 'tornado':
        from tornado.ioloop import IOLoop
        IOLoop.current().start()
    else:
        print('error: unknown server type %r' % server, file=sys.stderr)
        sys.exit(1)

    bgtasks.maybe_wait_for_threads_to_finish()