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


Python klein.Klein类代码示例

本文整理汇总了Python中klein.Klein的典型用法代码示例。如果您正苦于以下问题:Python Klein类的具体用法?Python Klein怎么用?Python Klein使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: test_resource

    def test_resource(self, mock_kr):
        """
        L{Klien.resource} returns a L{KleinResource}.
        """
        app = Klein()
        resource = app.resource()

        mock_kr.assert_called_with(app)
        self.assertEqual(mock_kr.return_value, resource)
开发者ID:djfroofy,项目名称:klein,代码行数:9,代码来源:test_app.py

示例2: test_runTCP6

 def test_runTCP6(self, reactor, mock_sfs, mock_log, mock_kr):
     """
     L{Klein.run} called with tcp6 endpoint description.
     """
     app = Klein()
     interface = "2001\:0DB8\:f00e\:eb00\:\:1"
     spec = "tcp6:8080:interface={0}".format(interface)
     app.run(endpoint_description=spec)
     reactor.run.assert_called_with()
     mock_sfs.assert_called_with(reactor, spec)
     mock_log.startLogging.assert_called_with(sys.stdout)
     mock_kr.assert_called_with(app)
开发者ID:joac,项目名称:klein,代码行数:12,代码来源:test_app.py

示例3: test_private_not_visible

    def test_private_not_visible(self):
        """
        When an endpoint is decorated with ``@private_api`` it is
        omitted from result of ``makeRst``.
        """
        app = Klein()

        @private_api
        def g():
            pass

        app.route(b"/g", methods=[b"GET"])(g)
        rest = list(makeRst(b"/", "section", app, None, {}))
        self.assertEqual(rest, [])
开发者ID:wangbinxiang,项目名称:flocker,代码行数:14,代码来源:test_publicapi.py

示例4: test_runSSL

 def test_runSSL(self, reactor, mock_sfs, mock_log, mock_kr):
     """
     L{Klein.run} called with SSL endpoint specification.
     """
     app = Klein()
     key = "key.pem"
     cert = "cert.pem"
     dh_params = "dhparam.pem"
     spec_template = "ssl:443:privateKey={0}:certKey={1}"
     spec = spec_template.format(key, cert, dh_params)
     app.run(endpoint_description=spec)
     reactor.run.assert_called_with()
     mock_sfs.assert_called_with(reactor, spec)
     mock_log.startLogging.assert_called_with(sys.stdout)
     mock_kr.assert_called_with(app)
开发者ID:joac,项目名称:klein,代码行数:15,代码来源:test_app.py

示例5: test_route

    def test_route(self):
        """
        L{Klein.route} adds functions as routable endpoints.
        """
        app = Klein()

        @app.route("/foo")
        def foo(request):
            return "foo"

        c = app.url_map.bind("foo")
        self.assertEqual(c.match("/foo"), ("foo", {}))
        self.assertEqual(len(app.endpoints), 1)

        self.assertEqual(app.execute_endpoint("foo", DummyRequest(1)), "foo")
开发者ID:Arttii,项目名称:deps,代码行数:15,代码来源:test_app.py

示例6: test_error_handlers_list_is_copied

    def test_error_handlers_list_is_copied(self):
        """
        L{Klein.__copy__} returns a new L{Klein} with all the error handlers
        """
        app = Klein()

        app.handle_errors(ValueError)(lambda request, failure: 'foo')

        app_copy = copy.copy(app)

        self.assertEquals(app._error_handlers, app_copy._error_handlers)

        app.handle_errors(KeyError)(lambda request, failure: 'foo')

        self.assertNotEquals(app._error_handlers, app_copy._error_handlers)
开发者ID:joac,项目名称:klein,代码行数:15,代码来源:test_app.py

示例7: test_runWithLogFile

    def test_runWithLogFile(self, reactor, mock_log, mock_site, mock_kr):
        """
        L{Klein.run} logs to the specified C{logFile}.
        """
        app = Klein()

        logFile = Mock()
        app.run("localhost", 8080, logFile=logFile)

        reactor.listenTCP.assert_called_with(8080, mock_site.return_value, interface="localhost")

        reactor.run.assert_called_with()

        mock_site.assert_called_with(mock_kr.return_value)
        mock_kr.assert_called_with(app)
        mock_log.startLogging.assert_called_with(logFile)
开发者ID:pdh,项目名称:klein,代码行数:16,代码来源:test_app.py

示例8: test_run

    def test_run(self, reactor, mock_log, mock_site, mock_kr):
        """
        L{Klein.run} configures a L{KleinResource} and a L{Site}
        listening on the specified interface and port, and logs
        to stdout.
        """
        app = Klein()

        app.run("localhost", 8080)

        reactor.listenTCP.assert_called_with(8080, mock_site.return_value, interface="localhost")

        reactor.run.assert_called_with()

        mock_site.assert_called_with(mock_kr.return_value)
        mock_kr.assert_called_with(app)
        mock_log.startLogging.assert_called_with(sys.stdout)
开发者ID:pdh,项目名称:klein,代码行数:17,代码来源:test_app.py

示例9: webserver_for_test

def webserver_for_test(test, url_path, response_content):
    """
    Create a webserver that serves ``response_content`` from ``url_path``.
    """
    app = Klein()

    @app.route(url_path)
    def _respond(request):
        return response_content
    factory = Site(app.resource())
    endpoint = serverFromString(reactor, b"tcp:0")
    listening = endpoint.listen(factory)

    def stop_port(port):
        test.addCleanup(port.stopListening)
        return port
    listening.addCallback(stop_port)
    return listening
开发者ID:ClusterHQ,项目名称:flocker,代码行数:18,代码来源:test_cinder.py

示例10: test_submountedRoute

    def test_submountedRoute(self):
        """
        L{Klein.subroute} adds functions as routable endpoints.
        """
        app = Klein()

        with app.subroute("/sub") as app:
            @app.route("/prefixed_uri")
            def foo_endpoint(request):
                return b"foo"

        c = app.url_map.bind("sub/prefixed_uri")
        self.assertEqual(
            c.match("/sub/prefixed_uri"), ("foo_endpoint", {}))
        self.assertEqual(
            len(app.endpoints), 1)
        self.assertEqual(
            app.execute_endpoint("foo_endpoint", DummyRequest(1)), b"foo")
开发者ID:Arttii,项目名称:deps,代码行数:18,代码来源:test_app.py

示例11: test_stackedRoute

    def test_stackedRoute(self):
        """
        L{Klein.route} can be stacked to create multiple endpoints of
        a single function.
        """
        app = Klein()

        @app.route("/foo")
        @app.route("/bar", endpoint="bar")
        def foobar(request):
            return "foobar"

        self.assertEqual(len(app.endpoints), 2)

        c = app.url_map.bind("foo")
        self.assertEqual(c.match("/foo"), ("foobar", {}))
        self.assertEqual(app.execute_endpoint("foobar", DummyRequest(1)), "foobar")

        self.assertEqual(c.match("/bar"), ("bar", {}))
        self.assertEqual(app.execute_endpoint("bar", DummyRequest(2)), "foobar")
开发者ID:Arttii,项目名称:deps,代码行数:20,代码来源:test_app.py

示例12: test_routes

    def test_routes(self):
        """
        L{getRoutes} returns all the defined routes and their attributes.
        """
        app = Klein()

        def f():
            pass
        f.attr = "attr"

        def g():
            pass
        g.attr = "G"
        app.route(b"/", methods=[b"GET"])(f)
        app.route(b"/g", methods=[b"PUT", b"OPTIONS"])(g)

        routes = sorted(getRoutes(app))
        self.assertEqual(routes, [
            KleinRoute(methods={b"GET"}, path=b"/", endpoint="f",
                       attributes={'attr': 'attr'}),
            KleinRoute(methods={b'PUT', b'OPTIONS'}, path=b'/g', endpoint='g',
                       attributes={'attr': 'G'})])
开发者ID:wangbinxiang,项目名称:flocker,代码行数:22,代码来源:test_publicapi.py

示例13: test_copy

    def test_copy(self):
        """
        L{Klein.__copy__} returns a new L{Klein} with all the registered endpoints
        """
        app = Klein()

        @app.route("/foo")
        def foo(request):
            return "foo"

        app_copy = copy.copy(app)

        @app.route('/bar')
        def bar(request):
            return 'bar'

        dr1 = DummyRequest(1)
        dr2 = DummyRequest(2)
        dr3 = DummyRequest(3)

        self.assertEquals(app.execute_endpoint('foo', dr1), 'foo')
        self.assertEquals(app.execute_endpoint('bar', dr2), 'bar')
        self.assertRaises(KeyError, app_copy.execute_endpoint, 'bar', dr3)
开发者ID:joac,项目名称:klein,代码行数:23,代码来源:test_app.py

示例14: main

def main(reactor):
    component = Component(
        transports=u"ws://localhost:8080/ws",
        realm=u"crossbardemo",
    )
    app = Klein()
    webapp = WebApplication(app, component)

    # have our Web site listen on 8090
    site = Site(app.resource())
    server_ep = TCP4ServerEndpoint(reactor, 8090)
    port = yield server_ep.listen(site)
    print("Web application on {}".format(port))

    # we don't *have* to hand over control of the reactor to
    # component.run -- if we don't want to, we call .start()
    # The Deferred it returns fires when the component is "completed"
    # (or errbacks on any problems).
    comp_d = component.start(reactor)

    # When not using run() we also must start logging ourselves.
    import txaio
    txaio.start_logging(level='info')

    # If the Component raises an exception we want to exit. Note that
    # things like failing to connect will be swallowed by the
    # re-connection mechanisms already so won't reach here.

    def _failed(f):
        print("Component failed: {}".format(f))
        done.errback(f)
    comp_d.addErrback(_failed)

    # wait forever (unless the Component raises an error)
    done = Deferred()
    yield done
开发者ID:crossbario,项目名称:autobahn-python,代码行数:36,代码来源:webapp.py

示例15: random_from_string

# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
# Cobble together a deterministic random function using a string as a seed.
from random import Random
from hashlib import sha256
from struct import unpack

def random_from_string(string):
    return Random(
        unpack("!I", sha256(string.encode("utf-8")).digest()[:4])[0]
    )

from twisted.web.template import tags, slot
from klein import Klein, Plating
app = Klein()

myStyle = Plating(
    tags=tags.html(
        tags.head(tags.title(slot("pageTitle"))),
        tags.body(tags.h1(slot("pageTitle"), Class="titleHeading"),
                  tags.div(slot(Plating.CONTENT)))
    ),
    defaults={"pageTitle": "Places & Foods"}
)

@myStyle.routed(
    app.route("/"),
    tags.div(
        tags.h2("Sample Places:"),
        tags.ul([tags.li(tags.a(href=["/places/", place])(place))
                 for place in ["new york", "san francisco", "shanghai"]]),
开发者ID:glasnt,项目名称:klein,代码行数:31,代码来源:html.py


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