本文整理汇总了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)
示例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)
示例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, [])
示例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)
示例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")
示例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)
示例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)
示例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)
示例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
示例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")
示例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")
示例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'})])
示例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)
示例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
示例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"]]),