本文整理汇总了Python中gns3server.controller.Controller.instance方法的典型用法代码示例。如果您正苦于以下问题:Python Controller.instance方法的具体用法?Python Controller.instance怎么用?Python Controller.instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gns3server.controller.Controller
的用法示例。
在下文中一共展示了Controller.instance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pcap
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def pcap(request, response):
project = yield from Controller.instance().get_loaded_project(request.match_info["project_id"])
link = project.get_link(request.match_info["link_id"])
while link.capture_file_path is None:
raise aiohttp.web.HTTPNotFound(text="pcap file not found")
while not os.path.isfile(link.capture_file_path):
yield from asyncio.sleep(0.5)
try:
with open(link.capture_file_path, "rb") as f:
response.content_type = "application/vnd.tcpdump.pcap"
response.set_status(200)
response.enable_chunked_encoding()
yield from response.prepare(request)
while True:
chunk = f.read(4096)
if not chunk:
yield from asyncio.sleep(0.1)
yield from response.write(chunk)
except OSError:
raise aiohttp.web.HTTPNotFound(text="pcap file {} not found or not accessible".format(link.capture_file_path))
示例2: get_file
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def get_file(request, response):
controller = Controller.instance()
project = yield from controller.get_loaded_project(request.match_info["project_id"])
path = request.match_info["path"]
path = os.path.normpath(path).strip('/')
# Raise error if user try to escape
if path[0] == ".":
raise aiohttp.web.HTTPForbidden
path = os.path.join(project.path, path)
response.content_type = "application/octet-stream"
response.set_status(200)
response.enable_chunked_encoding()
# Very important: do not send a content length otherwise QT closes the connection (curl can consume the feed)
response.content_length = None
try:
with open(path, "rb") as f:
response.start(request)
while True:
data = f.read(4096)
if not data:
break
yield from response.write(data)
except FileNotFoundError:
raise aiohttp.web.HTTPNotFound()
except PermissionError:
raise aiohttp.web.HTTPForbidden()
示例3: write_file
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def write_file(request, response):
controller = Controller.instance()
project = yield from controller.get_loaded_project(request.match_info["project_id"])
path = request.match_info["path"]
path = os.path.normpath(path).strip("/")
# Raise error if user try to escape
if path[0] == ".":
raise aiohttp.web.HTTPForbidden
path = os.path.join(project.path, path)
response.set_status(200)
try:
with open(path, 'wb+') as f:
while True:
packet = yield from request.content.read(512)
if not packet:
break
f.write(packet)
except FileNotFoundError:
raise aiohttp.web.HTTPNotFound()
except PermissionError:
raise aiohttp.web.HTTPForbidden()
except OSError as e:
raise aiohttp.web.HTTPConflict(text=str(e))
示例4: export_project
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def export_project(request, response):
controller = Controller.instance()
project = yield from controller.get_loaded_project(request.match_info["project_id"])
try:
with tempfile.TemporaryDirectory() as tmp_dir:
datas = yield from export_project(project, tmp_dir, include_images=bool(request.get("include_images", "0")))
# We need to do that now because export could failed and raise an HTTP error
# that why response start need to be the later possible
response.content_type = 'application/gns3project'
response.headers['CONTENT-DISPOSITION'] = 'attachment; filename="{}.gns3project"'.format(project.name)
response.enable_chunked_encoding()
# Very important: do not send a content length otherwise QT closes the connection (curl can consume the feed)
response.content_length = None
response.start(request)
for data in datas:
response.write(data)
yield from response.drain()
yield from response.write_eof()
# Will be raise if you have no space left or permission issue on your temporary directory
# RuntimeError: something was wrong during the zip process
except (OSError, RuntimeError) as e:
raise aiohttp.web.HTTPNotFound(text="Can't export project: {}".format(str(e)))
示例5: import_project
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def import_project(request, response):
controller = Controller.instance()
if request.get("path"):
config = Config.instance()
if config.get_section_config("Server").getboolean("local", False) is False:
response.set_status(403)
return
path = request.json.get("path")
name = request.json.get("name")
# We write the content to a temporary location and after we extract it all.
# It could be more optimal to stream this but it is not implemented in Python.
# Spooled means the file is temporary kept in memory until max_size is reached
try:
with tempfile.SpooledTemporaryFile(max_size=10000) as temp:
while True:
packet = yield from request.content.read(512)
if not packet:
break
temp.write(packet)
project = yield from import_project(controller, request.match_info["project_id"], temp, location=path, name=name)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e))
response.json(project)
response.set_status(201)
示例6: notification_ws
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def notification_ws(request, response):
controller = Controller.instance()
project = controller.get_project(request.match_info["project_id"])
ws = aiohttp.web.WebSocketResponse()
yield from ws.prepare(request)
asyncio.async(process_websocket(ws))
with controller.notification.queue(project) as queue:
while True:
try:
notification = yield from queue.get_json(5)
except asyncio.futures.CancelledError as e:
break
if ws.closed:
break
ws.send_str(notification)
if project.auto_close:
# To avoid trouble with client connecting disconnecting we sleep few seconds before checking
# if someone else is not connected
yield from asyncio.sleep(5)
if not controller.notification.project_has_listeners(project):
yield from project.close()
return ws
示例7: notification
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def notification(request, response):
controller = Controller.instance()
project = controller.get_project(request.match_info["project_id"])
response.content_type = "application/json"
response.set_status(200)
response.enable_chunked_encoding()
# Very important: do not send a content length otherwise QT closes the connection (curl can consume the feed)
response.content_length = None
response.start(request)
with controller.notification.queue(project) as queue:
while True:
try:
msg = yield from queue.get_json(5)
response.write(("{}\n".format(msg)).encode("utf-8"))
except asyncio.futures.CancelledError as e:
break
yield from response.drain()
if project.auto_close:
# To avoid trouble with client connecting disconnecting we sleep few seconds before checking
# if someone else is not connected
yield from asyncio.sleep(5)
if not controller.notification.project_has_listeners(project):
yield from project.close()
示例8: update
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def update(request, response):
project = yield from Controller.instance().get_loaded_project(request.match_info["project_id"])
link = project.get_link(request.match_info["link_id"])
yield from link.update_nodes(request.json["nodes"])
response.set_status(201)
response.json(link)
示例9: reload
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def reload(request, response):
project = yield from Controller.instance().get_loaded_project(request.match_info["project_id"])
node = project.get_node(request.match_info["node_id"])
yield from node.reload()
response.json(node)
response.set_status(201)
示例10: idlepc_proposals
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def idlepc_proposals(request, response):
project = yield from Controller.instance().get_loaded_project(request.match_info["project_id"])
node = project.get_node(request.match_info["node_id"])
idle = yield from node.dynamips_idlepc_proposals()
response.json(idle)
response.set_status(200)
示例11: raw
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def raw(request, response):
controller = Controller.instance()
try:
yield from response.file(controller.symbols.get_path(request.match_info["symbol_id"]))
except (KeyError, FileNotFoundError, PermissionError):
response.set_status(404)
示例12: delete
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
async def delete(request, response):
controller = Controller.instance()
project = controller.get_project(request.match_info["project_id"])
await project.delete()
controller.remove_project(project)
response.set_status(204)
示例13: update
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def update(request, response):
project = yield from Controller.instance().get_loaded_project(request.match_info["project_id"])
drawing = project.get_drawing(request.match_info["drawing_id"])
yield from drawing.update(**request.json)
response.set_status(201)
response.json(drawing)
示例14: shutdown
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
def shutdown(request, response):
config = Config.instance()
if config.get_section_config("Server").getboolean("local", False) is False:
raise HTTPForbidden(text="You can only stop a local server")
log.info("Start shutting down the server")
# close all the projects first
controller = Controller.instance()
projects = controller.projects.values()
tasks = []
for project in projects:
tasks.append(asyncio.async(project.close()))
if tasks:
done, _ = yield from asyncio.wait(tasks)
for future in done:
try:
future.result()
except Exception as e:
log.error("Could not close project {}".format(e), exc_info=1)
continue
# then shutdown the server itself
from gns3server.web.web_server import WebServer
server = WebServer.instance()
asyncio.async(server.shutdown_server())
response.set_status(201)
示例15: import_project
# 需要导入模块: from gns3server.controller import Controller [as 别名]
# 或者: from gns3server.controller.Controller import instance [as 别名]
async def import_project(request, response):
controller = Controller.instance()
if request.get("path"):
config = Config.instance()
if config.get_section_config("Server").getboolean("local", False) is False:
response.set_status(403)
return
path = request.json.get("path")
name = request.json.get("name")
# We write the content to a temporary location and after we extract it all.
# It could be more optimal to stream this but it is not implemented in Python.
try:
begin = time.time()
with tempfile.TemporaryDirectory() as tmpdir:
temp_project_path = os.path.join(tmpdir, "project.zip")
async with aiofiles.open(temp_project_path, 'wb') as f:
while True:
chunk = await request.content.read(CHUNK_SIZE)
if not chunk:
break
await f.write(chunk)
with open(temp_project_path, "rb") as f:
project = await import_project(controller, request.match_info["project_id"], f, location=path, name=name)
log.info("Project '{}' imported in {:.4f} seconds".format(project.name, time.time() - begin))
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e))
response.json(project)
response.set_status(201)