本文整理汇总了Python中klein.Klein.resource方法的典型用法代码示例。如果您正苦于以下问题:Python Klein.resource方法的具体用法?Python Klein.resource怎么用?Python Klein.resource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类klein.Klein
的用法示例。
在下文中一共展示了Klein.resource方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_resource
# 需要导入模块: from klein import Klein [as 别名]
# 或者: from klein.Klein import resource [as 别名]
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: webserver_for_test
# 需要导入模块: from klein import Klein [as 别名]
# 或者: from klein.Klein import resource [as 别名]
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
示例3: main
# 需要导入模块: from klein import Klein [as 别名]
# 或者: from klein.Klein import resource [as 别名]
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
示例4: square
# 需要导入模块: from klein import Klein [as 别名]
# 或者: from klein.Klein import resource [as 别名]
webapp.visits += 1
wampapp.session.publish(u'com.example.onvisit', visits=webapp.visits)
page = webapp.templates.get_template('index.html')
return page.render(visits=webapp.visits)
@webapp.route('/square/<int:x>')
@inlineCallbacks
def square(request, x):
result = yield wampapp.session.call(u'com.example.square', x)
page = webapp.templates.get_template('result.html')
content = page.render(x=x, result=result)
returnValue(content)
@webapp.route('/square/submit', methods=['POST'])
def square_submit(request):
x = int(request.args.get('x', [0])[0])
return square(request, x)
if __name__ == "__main__":
import sys
from twisted.python import log
from twisted.web.server import Site
from twisted.internet import reactor
log.startLogging(sys.stdout)
reactor.listenTCP(8080, Site(webapp.resource()))
wampapp.run(u"ws://127.0.0.1:9000", u"realm1", standalone=True)
示例5: transaction_id
# 需要导入模块: from klein import Klein [as 别名]
# 或者: from klein.Klein import resource [as 别名]
api,
"execute",
capability_version,
capability_hash, '')
links.append({"href": capability_url, "rel": "capability"})
return links
else:
return url
def transaction_id(request):
"""
Extract the transaction id from the given request.
:param IRequest request: The request we are trying to get the
transaction id for.
:returns: A string transaction id.
"""
return request.responseHeaders.getRawHeaders('X-Response-Id')[0]
app = Klein()
app.route = partial(app.route, strict_slashes=False)
root = Resource()
root.putChild('v1.0', app.resource())
root.putChild('', Data('', 'text/plain'))
示例6: FileNoDir
# 需要导入模块: from klein import Klein [as 别名]
# 或者: from klein.Klein import resource [as 别名]
# coding=utf-8
import os
from klein import Klein
from twisted.internet import reactor
from twisted.web.resource import Resource, ForbiddenResource
from twisted.web.server import Site
from twisted.web.static import File
from server.app import App
class FileNoDir(File):
def directoryListing(self):
return ForbiddenResource()
if __name__ == '__main__':
client_side_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'client_side'))
server = Klein()
app = App(server, client_side_dir)
root = server.resource()
site = Site(root)
"""
root.putChild("static", FileNoDir(os.path.join(client_side_dir, 'static')))
root.putChild("app", FileNoDir(os.path.join(client_side_dir, 'app')))
root.putChild("", File(os.path.join(client_side_dir, 'templates', 'index.html')))
root.putChild("templates", File(os.path.join(client_side_dir, 'templates')))
"""
reactor.listenTCP(8888, site)
reactor.run()
示例7: PlatingTests
# 需要导入模块: from klein import Klein [as 别名]
# 或者: from klein.Klein import resource [as 别名]
class PlatingTests(TestCase):
"""
Tests for L{Plating}.
"""
def setUp(self):
"""
Create an app and a resource wrapping that app for this test.
"""
self.app = Klein()
self.kr = self.app.resource()
def get(self, uri):
"""
Issue a virtual GET request to the given path that is expected to
succeed synchronously, and return the generated request object and
written bytes.
"""
request = requestMock(uri)
d = _render(self.kr, request)
self.successResultOf(d)
return request, request.getWrittenData()
def test_template_html(self):
"""
Rendering a L{Plating.routed} decorated route results in templated
HTML.
"""
@page.routed(self.app.route("/"),
tags.span(slot("ok")))
def plateMe(request):
return {"ok": "test-data-present"}
request, written = self.get(b"/")
self.assertIn(b'<span>test-data-present</span>', written)
self.assertIn(b'<title>default title unchanged</title>', written)
def test_template_json(self):
"""
Rendering a L{Plating.routed} decorated route with a query parameter
asking for JSON will yield JSON instead.
"""
@page.routed(self.app.route("/"),
tags.span(slot("ok")))
def plateMe(request):
return {"ok": "an-plating-test"}
request, written = self.get(b"/?json=true")
self.assertEqual(
request.responseHeaders.getRawHeaders(b'content-type')[0],
b'text/json; charset=utf-8'
)
self.assertEquals({"ok": "an-plating-test",
"title": "default title unchanged"},
json.loads(written.decode('utf-8')))
def test_template_numbers(self):
"""
Data returned from a plated method may include numeric types (integers,
floats, and possibly longs), which although they are not normally
serializable by twisted.web.template, will be converted by plating into
their decimal representation.
"""
@page.routed(self.app.route("/"),
tags.div(tags.span(slot("anInteger")),
tags.i(slot("anFloat")),
tags.b(slot("anLong")),
))
def plateMe(result):
return {"anInteger": 7,
"anFloat": 3.2,
"anLong": 0x10000000000000001}
request, written = self.get(b"/")
self.assertIn(b"<span>7</span>", written)
self.assertIn(b"<i>3.2</i>", written)
self.assertIn(b"<b>18446744073709551617</b>", written)
def test_render_list(self):
"""
The C{:list} renderer suffix will render the slot named by the renderer
as a list, filling each slot.
"""
@page.routed(self.app.route("/"),
tags.ul(tags.li(slot("item"),
render="subplating:list")))
def rsrc(request):
return {"subplating": [1, 2, 3]}
request, written = self.get(b"/")
self.assertIn(b'<ul><li>1</li><li>2</li><li>3</li></ul>', written)
self.assertIn(b'<title>default title unchanged</title>', written)
def test_widget_html(self):
"""
When L{Plating.widgeted} is applied as a decorator, it gives the
decorated function a C{widget} attribute which is a version of the
function with a modified return type that turns it into a renderable
HTML sub-element that may fill a slot.
"""
@page.routed(self.app.route("/"),
tags.div(slot("widget")))
def rsrc(request):
return {"widget": enwidget.widget(3, 4)}
#.........这里部分代码省略.........