def start(app): app.redis = client.Redis() app.SessionHandler = SessionHandler server = HTTPServer(app, xheaders=True) server.listen(options.port) server.redis = client.Redis() server.SessionHandler = SessionHandler install_tornado_shutdown_handler(IOLoop.instance(), server) logging.info("start service at port = " + str(options.port) + "\n") IOLoop.instance().start() logging.info('Done. Bye.')
def test_redis_lock_timeout(timeout, check_interval): connection = client.Redis() channel = str(random.random()) lock_a = redis.RedisLock(channel) lock_a.acquire(timeout=timeout, check_interval=check_interval) lock_b = redis.RedisLock(channel, connection=connection) with pytest.raises(portalocker.AlreadyLocked): try: lock_b.acquire(timeout=timeout, check_interval=check_interval) finally: lock_a.release() lock_a.connection.close()
def __init__(self): settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), xsrf_cookies=True, cookie_secret="---HUST-ISE---", login_url="/", autoload=False, ) handlers = [ (r"/", IndexHandler), (r"/(favicon\.ico)", tornado.web.StaticFileHandler, dict(path=settings['static_path'])), (r"/account/login", LoginHandler), (r"/account/logout", LogoutHandler), (r"/account/register", RegisterHandler), (r"/account/(\d*)", AccountHandler), (r"/self", SelfHandler), (r"/treatmentlist", TreatmentListHandler), (r"/treatment/(.*)", TreatmentHandler), (r"/exp/new", NewExpHandler), (r"/exp/newtreatment", NewTreatmentHandler), (r"/explist", ExpListHandler), (r"/exp/(\d+)", ExpIndexHandler), (r"/exp/(\d+)/settings", ExpSettingsHandler), (r"/exp/(\d+)/activate", ActivateExpHandler), (r"/exp/(\d+)/result", ExpResultHandler), (r"/exp/(\d+)/close", CloseExpHandler), (r"/exp/(\d+)/socket", ExpOnHandler), (r"/exp/(\d+)/train", ExpTrainHandler), (r"/exp/(\d+)/websocket", ExpSocketHandler), (r"/trainlist", TrainListHandler), (r"/train/([a-zA-Z0-9]*)", TrainHandler), (r"/train/([a-zA-Z0-9]*)/websocket", TrainSocketHandler), (r"/help", HelpHandler), ] super(Application, self).__init__(handlers, **settings) self.redis = client.Redis() self.db = torndb.Connection("localhost", 'exp', user='******', password='******')
def get_connection(self) -> client.Redis: if not self.connection: self.connection = client.Redis(**self.redis_kwargs) return self.connection
import logging import random import time import pytest from redis import client from redis import exceptions import portalocker from portalocker import redis from portalocker import utils logger = logging.getLogger(__name__) try: client.Redis().ping() except (exceptions.ConnectionError, ConnectionRefusedError): pytest.skip('Unable to connect to redis', allow_module_level=True) @pytest.fixture(autouse=True) def set_redis_timeouts(monkeypatch): monkeypatch.setattr(utils, 'DEFAULT_TIMEOUT', 0.0001) monkeypatch.setattr(utils, 'DEFAULT_CHECK_INTERVAL', 0.0005) monkeypatch.setattr(redis, 'DEFAULT_UNAVAILABLE_TIMEOUT', 0.01) monkeypatch.setattr(redis, 'DEFAULT_THREAD_SLEEP_TIME', 0.001) monkeypatch.setattr(_thread, 'interrupt_main', lambda: None) def test_redis_lock(): channel = str(random.random())
from redis import client r = client.Redis() r.publish('topic', 'message body') r.publish('topic2', 'message body')
"username") password = self.get_argument("password") if not self.current_password: if username == "abc" and password == "123": self.set_secure_cookie("username", username) self.session.password = password self.redirect("/") else: raise tornado.web.HTTPError(403, "user or password error") else: self.redirect("/") urls = [ (r"/", MainHandler), (r"/login", LoginHandler), ] settings = { "cookie_secret": "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", # 带签名的cookie "login_url": "/login", # "xsrf_cookies":"Ture", # 跨站伪造请求(Cross-site request forgery) 防范策略 xsrf_cookies } app = Application(urls, **settings) app.listen(8000) app.redis = client.Redis() app.SessionHandler = SessionHandler IOLoop.instance().start()
#!/usr/bin/env python3 # coding:utf-8 from route import route import tornado.web import os from utils.session import SessionHandler from redis import client setting = { "template_path": os.path.join(os.path.dirname(__file__), "template"), "static_path": os.path.join(os.path.dirname(__file__), "static"), "debug": True, # 带签名的cookie "cookie_secret": "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", # 跨站伪造请求(Cross-site request forgery) 防范策略 xsrf_cookies # "xsrf_cookies": "Ture", "login_url": "/login", } application = tornado.web.Application(route.handlers, **setting) application.redis = client.Redis() application.SessionHandler = SessionHandler