當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。