当前位置: 首页>>代码示例>>Python>>正文


Python sanic.Sanic方法代码示例

本文整理汇总了Python中sanic.Sanic方法的典型用法代码示例。如果您正苦于以下问题:Python sanic.Sanic方法的具体用法?Python sanic.Sanic怎么用?Python sanic.Sanic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sanic的用法示例。


在下文中一共展示了sanic.Sanic方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_unix_socket_creation

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def test_unix_socket_creation(caplog):
    from socket import AF_UNIX, socket

    with socket(AF_UNIX) as sock:
        sock.bind(SOCKPATH)
    assert os.path.exists(SOCKPATH)
    ino = os.stat(SOCKPATH).st_ino

    app = Sanic(name=__name__)

    @app.listener("after_server_start")
    def running(app, loop):
        assert os.path.exists(SOCKPATH)
        assert ino != os.stat(SOCKPATH).st_ino
        app.stop()

    with caplog.at_level(logging.INFO):
        app.run(unix=SOCKPATH)

    assert (
        "sanic.root",
        logging.INFO,
        f"Goin' Fast @ {SOCKPATH} http://...",
    ) in caplog.record_tuples
    assert not os.path.exists(SOCKPATH) 
开发者ID:huge-success,项目名称:sanic,代码行数:27,代码来源:test_unix_socket.py

示例2: test_unix_connection

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def test_unix_connection():
    app = Sanic(name=__name__)

    @app.get("/")
    def handler(request):
        return text(f"{request.conn_info.server}")

    @app.listener("after_server_start")
    async def client(app, loop):
        try:
            async with httpx.AsyncClient(uds=SOCKPATH) as client:
                r = await client.get("http://myhost.invalid/")
                assert r.status_code == 200
                assert r.text == os.path.abspath(SOCKPATH)
        finally:
            app.stop()

    app.run(host="myhost.invalid", unix=SOCKPATH) 
开发者ID:huge-success,项目名称:sanic,代码行数:20,代码来源:test_unix_socket.py

示例3: write_app

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def write_app(filename, **runargs):
    text = secrets.token_urlsafe()
    with open(filename, "w") as f:
        f.write(
            dedent(
                f"""\
            import os
            from sanic import Sanic

            app = Sanic(__name__)

            @app.listener("after_server_start")
            def complete(*args):
                print("complete", os.getpid(), {text!r})

            if __name__ == "__main__":
                app.run(**{runargs!r})
            """
            )
        )
    return text 
开发者ID:huge-success,项目名称:sanic,代码行数:23,代码来源:test_reloader.py

示例4: test_logging_defaults

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def test_logging_defaults():
    # reset_logging()
    app = Sanic("test_logging")

    for fmt in [h.formatter for h in logging.getLogger("sanic.root").handlers]:
        assert (
            fmt._fmt
            == LOGGING_CONFIG_DEFAULTS["formatters"]["generic"]["format"]
        )

    for fmt in [
        h.formatter for h in logging.getLogger("sanic.error").handlers
    ]:
        assert (
            fmt._fmt
            == LOGGING_CONFIG_DEFAULTS["formatters"]["generic"]["format"]
        )

    for fmt in [
        h.formatter for h in logging.getLogger("sanic.access").handlers
    ]:
        assert (
            fmt._fmt
            == LOGGING_CONFIG_DEFAULTS["formatters"]["access"]["format"]
        ) 
开发者ID:huge-success,项目名称:sanic,代码行数:27,代码来源:test_logging.py

示例5: test_logging_pass_customer_logconfig

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def test_logging_pass_customer_logconfig():
    # reset_logging()

    modified_config = LOGGING_CONFIG_DEFAULTS
    modified_config["formatters"]["generic"][
        "format"
    ] = "%(asctime)s - (%(name)s)[%(levelname)s]: %(message)s"
    modified_config["formatters"]["access"][
        "format"
    ] = "%(asctime)s - (%(name)s)[%(levelname)s]: %(message)s"

    app = Sanic("test_logging", log_config=modified_config)

    for fmt in [h.formatter for h in logging.getLogger("sanic.root").handlers]:
        assert fmt._fmt == modified_config["formatters"]["generic"]["format"]

    for fmt in [
        h.formatter for h in logging.getLogger("sanic.error").handlers
    ]:
        assert fmt._fmt == modified_config["formatters"]["generic"]["format"]

    for fmt in [
        h.formatter for h in logging.getLogger("sanic.access").handlers
    ]:
        assert fmt._fmt == modified_config["formatters"]["access"]["format"] 
开发者ID:huge-success,项目名称:sanic,代码行数:27,代码来源:test_logging.py

示例6: test_unauthorized_exception

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def test_unauthorized_exception(exception_app):
    """Test the built-in Unauthorized exception"""
    request, response = exception_app.test_client.get("/401")
    assert response.status == 401

    request, response = exception_app.test_client.get("/401/basic")
    assert response.status == 401
    assert response.headers.get("WWW-Authenticate") is not None
    assert response.headers.get("WWW-Authenticate") == 'Basic realm="Sanic"'

    request, response = exception_app.test_client.get("/401/digest")
    assert response.status == 401

    auth_header = response.headers.get("WWW-Authenticate")
    assert auth_header is not None
    assert auth_header.startswith("Digest")
    assert 'qop="auth, auth-int"' in auth_header
    assert 'algorithm="MD5"' in auth_header
    assert 'nonce="abcdef"' in auth_header
    assert 'opaque="zyxwvu"' in auth_header

    request, response = exception_app.test_client.get("/401/bearer")
    assert response.status == 401
    assert response.headers.get("WWW-Authenticate") == "Bearer" 
开发者ID:huge-success,项目名称:sanic,代码行数:26,代码来源:test_exceptions.py

示例7: test_config_with_cookie_domain

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def test_config_with_cookie_domain(users, authenticate):
    domain = "cookie.yum"
    sanic_app = Sanic("sanic-jwt-test")
    Initialize(
        sanic_app,
        authenticate=authenticate,
        cookie_set=True,
        cookie_domain=domain,
    )

    _, response = sanic_app.test_client.post(
        "/auth",
        json={"username": "user1", "password": "abcxyz"},
        raw_cookies=True,
    )

    cookie = response.raw_cookies.get("access_token")
    assert cookie.domain == domain 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:20,代码来源:test_endpoints_cookies.py

示例8: test_redirect_with_configured_url

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def test_redirect_with_configured_url():
    sanic_app = Sanic("sanic-jwt-test")
    sanic_jwt = Initialize(
        sanic_app, auth_mode=False, login_redirect_url="/unprotected"
    )

    @sanic_app.route("/protected/static")
    @sanic_jwt.protected(redirect_on_fail=True)
    async def my_protected_static(request):
        return text("", status=200)

    @sanic_app.route("/unprotected")
    async def my_unprotected_goto(request):
        return text("unprotected content", status=200)

    _, response = sanic_app.test_client.get("/protected/static")

    assert response.status == 200 and response.text == "unprotected content" 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:20,代码来源:test_decorators.py

示例9: app_with_user_secrets

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def app_with_user_secrets(username_table, authenticate, retrieve_user_secret):

    sanic_app = Sanic("sanic-jwt-test")
    sanic_jwt = Initialize(
        sanic_app,
        authenticate=authenticate,
        user_secret_enabled=True,
        retrieve_user_secret=retrieve_user_secret,
    )

    @sanic_app.route("/protected")
    @protected()
    async def protected_request(request):
        return json({"protected": True})

    yield (sanic_app, sanic_jwt) 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:18,代码来源:conftest.py

示例10: app_with_url_prefix

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def app_with_url_prefix(username_table, authenticate):

    sanic_app = Sanic("sanic-jwt-test")
    sanic_jwt = Initialize(
        sanic_app, authenticate=authenticate, url_prefix="/somethingelse"
    )

    @sanic_app.route("/")
    async def helloworld(request):
        return json({"hello": "world"})

    @sanic_app.route("/protected")
    @protected()
    async def protected_request(request):
        return json({"protected": True})

    yield (sanic_app, sanic_jwt) 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:19,代码来源:conftest.py

示例11: app_with_bp_setup_without_init

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def app_with_bp_setup_without_init(username_table, authenticate):
    sanic_app = Sanic("sanic-jwt-test")

    @sanic_app.route("/")
    async def helloworld(request):
        return json({"hello": "world"})

    @sanic_app.route("/protected")
    @protected()
    async def protected_request(request):
        return json({"protected": True})

    sanic_bp = Blueprint("bp", url_prefix="/bp")

    @sanic_bp.route("/")
    async def bp_helloworld(request):
        return json({"hello": "world"})

    @sanic_bp.route("/protected")
    @protected()
    async def bp_protected_request(request):
        return json({"protected": True})

    yield (sanic_app, sanic_bp) 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:26,代码来源:conftest.py

示例12: app_with_leeway

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def app_with_leeway(username_table, authenticate):

    sanic_app = Sanic("sanic-jwt-test")
    sanic_jwt = Initialize(
        sanic_app, authenticate=authenticate, leeway=(60 * 5)
    )

    @sanic_app.route("/")
    async def helloworld(request):
        return json({"hello": "world"})

    @sanic_app.route("/protected")
    @protected()
    async def protected_request(request):
        return json({"protected": True})

    yield (sanic_app, sanic_jwt) 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:19,代码来源:conftest.py

示例13: app_with_nbf

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def app_with_nbf(username_table, authenticate):

    sanic_app = Sanic("sanic-jwt-test")
    sanic_jwt = Initialize(
        sanic_app,
        authenticate=authenticate,
        claim_nbf=True,
        claim_nbf_delta=(60 * 5),
    )

    @sanic_app.route("/")
    async def helloworld(request):
        return json({"hello": "world"})

    @sanic_app.route("/protected")
    @protected()
    async def protected_request(request):
        return json({"protected": True})

    yield (sanic_app, sanic_jwt) 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:22,代码来源:conftest.py

示例14: app_with_iat

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def app_with_iat(username_table, authenticate):

    sanic_app = Sanic("sanic-jwt-test")
    sanic_jwt = Initialize(
        sanic_app, authenticate=authenticate, claim_iat=True
    )

    @sanic_app.route("/")
    async def helloworld(request):
        return json({"hello": "world"})

    @sanic_app.route("/protected")
    @protected()
    async def protected_request(request):
        return json({"protected": True})

    yield (sanic_app, sanic_jwt) 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:19,代码来源:conftest.py

示例15: app_with_iss

# 需要导入模块: import sanic [as 别名]
# 或者: from sanic import Sanic [as 别名]
def app_with_iss(username_table, authenticate):

    sanic_app = Sanic("sanic-jwt-test")
    sanic_jwt = Initialize(
        sanic_app, authenticate=authenticate, claim_iss="issuingserver"
    )

    @sanic_app.route("/")
    async def helloworld(request):
        return json({"hello": "world"})

    @sanic_app.route("/protected")
    @protected()
    async def protected_request(request):
        return json({"protected": True})

    yield (sanic_app, sanic_jwt) 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:19,代码来源:conftest.py


注:本文中的sanic.Sanic方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。