本文整理匯總了Python中router.Router.route方法的典型用法代碼示例。如果您正苦於以下問題:Python Router.route方法的具體用法?Python Router.route怎麽用?Python Router.route使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類router.Router
的用法示例。
在下文中一共展示了Router.route方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Executor
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import route [as 別名]
class Executor(object):
def __init__(self,cmd,user,proto):
self.argv = shlex.split(cmd)
self.user = user
self.proto = proto
self.router = Router(self.argv[-1])
self.authobj = Authenticator(user,self.argv)
self.envobj = Env(self.user,self.router)
def execute(self):
repostring = self.argv[-1]
self.router.route()
auth_service_deferred = self.router.deferred
auth_service_deferred.addCallback(self.authobj.getRepositoryAccess)
auth_service_deferred.addCallback(self.envobj.setEnv)
# Then the result of auth is passed to execGitCommand to run git-shell
auth_service_deferred.addCallback(self.execGitCommand, self.argv, self.proto)
auth_service_deferred.addErrback(self.errorHandler, self.proto)
def execGitCommand(self, env , argv, proto):
"""After all authentication is done, setup an environment and execute the git-shell commands."""
repopath = self.router.repopath
sh = self.user.shell
command = ' '.join(argv[:-1] + ["'{0}'".format(repopath)])
reactor.spawnProcess(proto, sh, (sh, '-c', command), env=env)
def errorHandler(self, fail, proto):
"""Catch any unhandled errors and send the exception string to the remote client."""
fail.trap(ConchError)
message = fail.value.value
log.err(message)
if proto.connectionMade():
proto.loseConnection()
error_script = self.user.error_script
reactor.spawnProcess(proto, error_script, (error_script, message))
示例2: test3
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import route [as 別名]
def test3(self):
r = Router()
r.route("/pages/", target="a")
r.route(Rule("/pages/"), target="b")
matches = r.matches("/pages/")
self.assertEqual(matches.next().param("target"), "a")
self.assertEqual(matches.next().param("target"), "b")
示例3: test2
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import route [as 別名]
def test2(self):
r = Router()
r.route(Rule("/pages/<int:page_id>/"), target="pages")
matches = r.matches("/pages/10/")
m = matches.next()
self.assertEqual(m.param("target"), "pages")
self.assertEqual(m.param("page_id"), 10)
示例4: Handler
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import route [as 別名]
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
self.router = Router(self)
for key, value in ROUTES.items():
self.router.add(key, value)
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server)
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
if self.path.startswith('/'):
self.path = self.path[1:]
routing_info = self.router.route(self.path)
if routing_info:
func, regex_match = routing_info
content = func(**regex_match)
self.do_HEAD()
self.wfile.write(content)
else:
self.send_error(404)
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
# Begin the response
self.send_response(200)
self.end_headers()
self.wfile.write('Client: %s\n' % str(self.client_address))
self.wfile.write('User-agent: %s\n' % str(self.headers['user-agent']))
self.wfile.write('Path: %s\n' % self.path)
self.wfile.write('Form data:\n')
# Echo back information about what was posted in the form
for field in form.keys():
field_item = form[field]
if field_item.filename:
# The field contains an uploaded file
file_data = field_item.file.read()
file_len = len(file_data)
del file_data
self.wfile.write('\tUploaded %s as "%s" (%d bytes)\n' % \
(field, field_item.filename, file_len))
else:
# Regular form value
self.wfile.write('\t%s=%s\n' % (field, form[field].value))
return
示例5: test1
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import route [as 別名]
def test1(self):
r = Router()
r.route(Rule("/blaat/"), target="0")
r.route(Rule("/home/"), target="1")
r.route(Rule("/news/"), target="2")
r.route(Rule("/home/"), target="3")
matches = r.matches("/home/")
self.assertEqual(matches.next().param("target"), "1")
self.assertEqual(matches.next().param("target"), "3")
示例6: Router
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import route [as 別名]
global res_status
global res_headers
res_status=status
res_headers=response_headers
router = Router()
#here is the application
@router('/hello')
def hello(environ, start_response):
status = '200 OK'
output = 'Hello'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
write = start_response(status, response_headers)
return [output]
@router('/world')
def world(environ, start_response):
status = '200 OK'
output = 'World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
write = start_response(status, response_headers)
return [output]
#here run the application
result = router.route(environ, start_response)
for value in result:
write(value)