本文整理汇总了Python中bottle.ServerAdapter方法的典型用法代码示例。如果您正苦于以下问题:Python bottle.ServerAdapter方法的具体用法?Python bottle.ServerAdapter怎么用?Python bottle.ServerAdapter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bottle
的用法示例。
在下文中一共展示了bottle.ServerAdapter方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _start_server
# 需要导入模块: import bottle [as 别名]
# 或者: from bottle import ServerAdapter [as 别名]
def _start_server(self):
class BottleServerAdapter(bottle.ServerAdapter):
proxy = self
def close_session(self):
self.proxy.ctx.model.log._session.remove()
def run(self, app):
class Server(wsgiref.simple_server.WSGIServer):
allow_reuse_address = True
bottle_server = self
def handle_error(self, request, client_address):
pass
def serve_forever(self, poll_interval=0.5):
try:
wsgiref.simple_server.WSGIServer.serve_forever(self, poll_interval)
finally:
# Once shutdown is called, we need to close the session.
# If the session is not closed properly, it might raise warnings,
# or even lock the database.
self.bottle_server.close_session()
class Handler(wsgiref.simple_server.WSGIRequestHandler):
def address_string(self):
return self.client_address[0]
def log_request(*args, **kwargs): # pylint: disable=no-method-argument
if not self.quiet:
return wsgiref.simple_server.WSGIRequestHandler.log_request(*args,
**kwargs)
server = wsgiref.simple_server.make_server(
host=self.host,
port=self.port,
app=app,
server_class=Server,
handler_class=Handler)
self.proxy.server = server
self.proxy._started.put(True)
server.serve_forever(poll_interval=0.1)
def serve():
# Since task is a thread_local object, we need to patch it inside the server thread.
self._ctx_patcher(self.ctx)
bottle_app = bottle.Bottle()
bottle_app.post('/', callback=self._request_handler)
bottle.run(
app=bottle_app,
host='localhost',
port=self.port,
quiet=True,
server=BottleServerAdapter)
thread = threading.Thread(target=serve)
thread.daemon = True
thread.start()
return thread
示例2: _start_server
# 需要导入模块: import bottle [as 别名]
# 或者: from bottle import ServerAdapter [as 别名]
def _start_server(self):
proxy = self
class BottleServerAdapter(bottle.ServerAdapter):
def run(self, app):
class Server(WSGIServer):
allow_reuse_address = True
def handle_error(self, request, client_address):
pass
class Handler(WSGIRequestHandler):
def address_string(self):
return self.client_address[0]
def log_request(*args, **kwargs):
if not self.quiet:
return WSGIRequestHandler.log_request(
*args, **kwargs)
self.srv = make_wsgi_server(
self.host,
self.port,
app,
Server,
Handler)
proxy.server = self.srv
self.port = self.srv.server_port
proxy._started.put(True)
self.srv.serve_forever(poll_interval=0.1)
bottle.post('/', callback=self._request_handler)
def serve():
bottle.run(
host='localhost',
port=self.port,
quiet=True,
server=BottleServerAdapter)
thread = threading.Thread(target=serve)
thread.daemon = True
thread.start()
return thread
示例3: authenticate
# 需要导入模块: import bottle [as 别名]
# 或者: from bottle import ServerAdapter [as 别名]
def authenticate(oauth_class=OAuth2):
class StoppableWSGIServer(bottle.ServerAdapter):
def __init__(self, *args, **kwargs):
super(StoppableWSGIServer, self).__init__(*args, **kwargs)
self._server = None
def run(self, app):
server_cls = self.options.get('server_class', WSGIServer)
handler_cls = self.options.get('handler_class', WSGIRequestHandler)
self._server = make_server(self.host, self.port, app, server_cls, handler_cls)
self._server.serve_forever()
def stop(self):
self._server.shutdown()
auth_code = {}
auth_code_is_available = Event()
local_oauth_redirect = bottle.Bottle()
@local_oauth_redirect.get('/')
def get_token():
auth_code['auth_code'] = bottle.request.query.code
auth_code['state'] = bottle.request.query.state
auth_code_is_available.set()
local_server = StoppableWSGIServer(host='localhost', port=8080)
server_thread = Thread(target=lambda: local_oauth_redirect.run(server=local_server))
server_thread.start()
oauth = oauth_class(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
)
auth_url, csrf_token = oauth.get_authorization_url('http://localhost:8080')
webbrowser.open(auth_url)
auth_code_is_available.wait()
local_server.stop()
assert auth_code['state'] == csrf_token
access_token, refresh_token = oauth.authenticate(auth_code['auth_code'])
print('access_token: ' + access_token)
print('refresh_token: ' + refresh_token)
return oauth, access_token, refresh_token