本文整理汇总了Python中explorer.utils.misc.Response类的典型用法代码示例。如果您正苦于以下问题:Python Response类的具体用法?Python Response怎么用?Python Response使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Response类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: login_handler
def login_handler(request):
""" HTTP Request handler function for user login / logout requests """
session = ET.Element('session')
if request.POST:
action = request.POST['action']
if action == 'login':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
# Correct password, and the user is marked "active"
login(request, user)
session.text = username
else:
return HttpResponse(Response.error('login', 'Authentication Failed'))
else:
try:
if request.session.session_key is not None and request.session.session_key != '':
session_dir = ServerSettings.session_path(request.session.session_key)
if os.path.exists(session_dir):
logging.debug('Cleaning ' + session_dir)
shutil.rmtree(session_dir)
logout(request)
except Exception as ex:
print(ex.__doc__)
print(ex.message)
logging.debug('Login success')
return HttpResponse(Response.success(action, 'ok', session))
return HttpResponse(Response.error(action, 'Invalid Request'))
开发者ID:MFALHI,项目名称:yang-explorer,代码行数:29,代码来源:views.py
示例2: login_handler
def login_handler(request):
""" HTTP Request handler function for user login / logout requests """
if request.POST:
action = request.POST['action']
if action == 'login':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
# Correct password, and the user is marked "active"
login(request, user)
else:
return HttpResponse(Response.error('login', 'Authentication Failed'))
else:
username = ''
try:
if request.session.session_key is not None and request.session.session_key != '':
session_dir = ServerSettings.session_path(request.session.session_key)
if os.path.exists(session_dir):
logger.debug('Cleaning ' + session_dir)
shutil.rmtree(session_dir)
logout(request)
except:
logger.exception("Failed")
else:
logger.debug('Logout success!!')
session = get_session_config(username)
return HttpResponse(Response.success(action, 'ok', session))
return HttpResponse(Response.error('unknown', 'Invalid request!!'))
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:29,代码来源:views.py
示例3: upload_handler
def upload_handler(request):
""" HTTP Request handler function to upload yang models """
mode = request.GET.get('mode', '')
logging.debug(request.method + ':Received upload request .. ' + mode)
if not request.user.is_authenticated():
logging.warning('User must be logged in !!')
return HttpResponse(Response.error(mode, 'Unauthorized'))
if not ServerSettings.user_aware():
if not request.user.has_perm('explorer.delete_yangmodel') or \
not request.user.has_perm('explorer.change_yangmodel'):
logging.warning('Unauthorized upload request .. ')
return HttpResponse(Response.error(mode, 'User does not have permission to upload !!'))
if request.method == 'POST':
# create a temporary storage for this session
directory = ServerSettings.session_path(request.session.session_key)
_file = Uploader.upload_file(request.FILES['Filedata'], directory)
if _file is not None:
module = ET.Element('module')
module.text = _file
rval = Response.success('upload', 'ok', xml=module)
logging.debug(rval)
return HttpResponse(rval)
return HttpResponse(Response.error('upload', 'Failed to upload'))
elif request.method == 'GET':
if mode == 'sync':
filename = request.GET.get('file', '')
index = request.GET.get('index', '')
logging.info('Received sync request for ' + filename + ', index ' + index)
success, response = Uploader.sync_file(request.user.username,
request.session.session_key,
filename, index)
if success:
return HttpResponse(Response.success(mode, 'ok'))
return HttpResponse(Response.error(mode, 'compilation failed', xml=response))
elif mode == 'commit':
success, modules = Uploader.commit_files(request.user.username, request.session.session_key)
if success:
return HttpResponse(Response.success('commit', 'ok', modules))
elif mode == 'init':
success, modules = Uploader.get_upload_files(request.user.username, request.session.session_key)
if success:
return HttpResponse(Response.success(mode, 'ok', modules))
elif mode == 'clear':
success, modules = Uploader.clear_upload_files(request.user.username, request.session.session_key)
if success:
return HttpResponse(Response.success(mode, 'ok', modules))
return HttpResponse(Response.error(mode, 'failed'))
return render_to_response('upload.html')
开发者ID:center3d,项目名称:yang-explorer,代码行数:56,代码来源:views.py
示例4: request_handler
def request_handler(request):
""" HTTP Request handler function to handle actions on collections """
if not request.user.is_authenticated():
return HttpResponse(Response.error(None, 'User must be logged in'))
mode = request.GET.get('mode', '')
reply_xml = None
logger.info('request_handler: Received Request: (%s)' % mode)
if mode == 'get-collection-list':
reply_xml = Collection.list()
elif mode == 'load-collection':
metadata = request.GET.get('metadata', '')
reply_xml = Collection.load(request.user.username, metadata)
if reply_xml is None:
return HttpResponse(Response.error(mode, 'Failed'))
if isinstance(reply_xml, str):
return HttpResponse(Response.success(mode, reply_xml))
elif mode == 'add-collection':
metadata = request.GET.get('metadata', '')
payload = request.GET.get('payload', '')
logger.debug('metadata: ' + metadata)
logger.debug('payload: ' + payload)
if not Collection.add(metadata, payload):
return HttpResponse(Response.error(mode, 'Failed'))
reply_xml = Collection.list()
mode = 'get-collection-list'
elif mode == 'delete-collection':
metadata = request.GET.get('metadata', '')
if not Collection.remove(metadata):
return HttpResponse(Response.error(mode, 'Failed'))
reply_xml = Collection.list()
mode = 'get-collection-list'
elif mode == 'rpc':
req = request.GET.get('payload', '')
reply_xml = Adapter.gen_rpc(request.user.username, req)
if isinstance(reply_xml, str):
return HttpResponse(Response.success(mode, reply_xml))
elif mode == 'gen-script':
req = request.GET.get('payload', '')
reply_xml = Adapter.gen_script(request.user.username, req)
if isinstance(reply_xml, str):
return HttpResponse(Response.success(mode, reply_xml))
elif mode in ['get-cap', 'run-rpc', 'run-edit-commit', 'run-commit']:
payload = request.GET.get('payload', '')
logger.debug('run: ' + payload)
reply_xml = Adapter.run_request(request.user.username, payload)
if isinstance(reply_xml, str):
return HttpResponse(Response.success(mode, reply_xml))
return HttpResponse(Response.success(mode, 'ok', reply_xml))
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:60,代码来源:views.py
示例5: download_schema
def download_schema(request, req):
"""
This API download yang schema from device and bundle it
"""
logger.debug("Download Schemas")
modules = download_yang(request, req)
session_dir = ServerSettings.schema_path(request.session.session_key)
http_host = request.META["HTTP_HOST"]
current = str(datetime.now())
current = current.replace(" ", "-")
current = current[: current.rindex(".")]
zfname = "schema-" + current + ".zip"
zfile = session_dir + "/" + zfname
homedir = os.getcwd()
os.chdir(session_dir)
with ZipFile(zfile, "w") as lz:
for f in glob.glob("*.yang"):
lz.write(f)
os.remove(f)
if not lz.namelist():
os.remove(zfile)
os.chdir(homedir)
url = "\nhttp://" + http_host + "/" + "download/session/"
url += request.session.session_key + "/" + zfname
return HttpResponse(Response.success("download", msg=url))
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:29,代码来源:schema.py
示例6: add_schema
def add_schema(request, req):
'''
This API download yang schema from device
'''
logging.debug('Add Schemas')
modules = download_yang(request, req)
return HttpResponse(Response.success('add', 'ok', xml=modules))
开发者ID:center3d,项目名称:yang-explorer,代码行数:7,代码来源:schema.py
示例7: add_schema
def add_schema(request, req):
"""
This API download yang schema from device
"""
logger.debug("Add Schemas")
modules = download_yang(request, req)
return HttpResponse(Response.success("add", "ok", xml=modules))
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:7,代码来源:schema.py
示例8: download_schema
def download_schema(request, req):
'''
This API download yang schema from device and bundle it
'''
logging.debug('Download Schemas')
modules = download_yang(request, req)
session_dir = ServerSettings.schema_path(request.session.session_key)
http_host = request.META['HTTP_HOST']
current = str(datetime.now())
current = current.replace(' ', '-')
current = current[:current.rindex('.')]
zfname = 'schema-' + current + '.zip'
zfile = session_dir + '/' + zfname
homedir = os.getcwd()
os.chdir(session_dir)
with ZipFile(zfile, "w") as lz:
for f in glob.glob("*.yang"):
lz.write(f)
os.remove(f)
if not lz.namelist():
os.remove(zfile)
os.chdir(homedir)
url = '\nhttp://' + http_host + '/' + 'download/session/' + request.session.session_key + '/' + zfname
return HttpResponse(Response.success('download', msg=url))
开发者ID:center3d,项目名称:yang-explorer,代码行数:28,代码来源:schema.py
示例9: download_yang
def download_yang(request, req):
'''
This API download yang schema from device
'''
logging.debug('Download Yang Schema')
req = req.replace('<metadata>', '')
req = req.replace('</metadata>', '')
protocol, device, fmt, payload = Adapter.parse_request(req)
if device.get('host', None) is None:
return HttpResponse(Response.error('download', 'no host info'))
session_dir = ServerSettings.schema_path(request.session.session_key)
if not os.path.exists(session_dir):
os.makedirs(session_dir)
if not os.path.exists(session_dir):
return HttpResponse(Response.error('download', 'No session directory'))
for fname in os.listdir(session_dir):
if fname.endswith('.yang'):
fn = os.path.join(session_dir, fname)
os.remove(fn)
modules = ET.Element('modules')
reqxml = ET.fromstring(req)
schemas = reqxml.find('schemas')
for sc in schemas:
id = sc.text
module = ET.Element('module')
get_sc = ET.Element('get-schema')
get_sc.set("xmlns", "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring")
identifier = ET.Element("identifier")
sfile = id.split('@')[0]
identifier.text = sfile
module.text = id+'.yang'
get_sc.append(identifier)
rpc.append(get_sc)
modules.append(module)
schema = Adapter.run_netconf(request.user.username, device, rpc)
fname = os.path.join(session_dir, id+'.yang')
with open(fname, 'w') as f:
f.write(schema[0][0].text)
rpc.remove(get_sc)
return modules
开发者ID:center3d,项目名称:yang-explorer,代码行数:46,代码来源:schema.py
示例10: session_handler
def session_handler(request):
""" HTTP Request handler function to check if user session exists """
if request.user.is_authenticated():
session = get_session_config(request.user.username)
else:
session = get_session_config('')
return HttpResponse(Response.success('session', 'ok', session))
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:8,代码来源:views.py
示例11: download_yang
def download_yang(request, req):
"""
This API download yang schema from device
"""
logger.debug("Download Yang Schema")
req = req.replace("<metadata>", "")
req = req.replace("</metadata>", "")
protocol, device, fmt, _, payload = Adapter.parse_request(req)
if device.get("host", None) is None:
return HttpResponse(Response.error("download", "Netconf agent address missing!!"))
# clear session directory if it exists
Uploader.clear_upload_files(None, request.session.session_key)
# create session directory if it does not exist
session_dir = Uploader.create_session_storage(request.session.session_key)
if session_dir is None:
logger.error("download_yang: Invalid session")
return HttpResponse(Response.error("download", "Invalid session_id"))
# extact list of models from request
req_xml = ET.fromstring(req)
models = [sc.text.strip() for sc in req_xml.find("schemas")]
# download all models recursively
rpc = ET.fromstring(get_schema_rpc)
download_helper(request.user.username, device, session_dir, rpc, models)
# prepare list of downloaded models
modules = ET.Element("modules")
for _file in glob.glob(os.path.join(session_dir, "*.yang")):
# see if we need to rename file with revision date
parser = Parser(_file)
new_fname = os.path.join(session_dir, parser.get_filename())
if _file != new_fname:
os.rename(_file, new_fname)
module = ET.Element("module")
module.text = os.path.basename(new_fname)
modules.append(module)
return modules
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:44,代码来源:schema.py
示例12: admin_handler
def admin_handler(request):
""" HTTP Request handler function to handle actions on yang modules """
if not request.user.is_authenticated():
return HttpResponse(Response.error(None, 'User must be logged in'))
if request.method != 'GET':
return HttpResponse(Response.error(None, 'Invalid admin Request'))
action = request.GET.get('action', '')
logging.debug('Received admin request %s for user %s' % (action, request.user.username))
if action in ['subscribe', 'unsubscribe', 'delete']:
payload = request.GET.get('payload', None)
if not ModuleAdmin.admin_action(request.user.username, payload, action):
return HttpResponse(Response.error(action, 'Failed to %s' % action))
modules = ModuleAdmin.get_modules(request.user.username)
return HttpResponse(Response.success('modulelist', 'ok', xml=modules))
开发者ID:janlindblad,项目名称:yang-explorer,代码行数:19,代码来源:views.py
示例13: get_schema
def get_schema(request, req, all=False):
'''
This API get yang schema from device
'''
logging.debug('Get Yang Schema')
req = req.replace('<metadata>', '')
req = req.replace('</metadata>', '')
protocol, device, fmt, payload = Adapter.parse_request(req)
if device.get('host', None) is None:
return HttpResponse(Response.error('get', 'no host info'))
rpc_xml= ET.fromstring(get_schema_rpc)
xml = Adapter.run_netconf(request.user.username, device, rpc_xml)
if xml is None:
return HttpResponse(Response.error('get', 'failed to get schema'))
if xml.text is not None:
if xml.text.find('error'):
return HttpResponse(Response.error('get', xml.text))
sclist = xml[0][0][0][0]
schemas = ET.Element('schemas')
for sc in sclist:
schema = ET.Element('schema')
id = sc[0].text
ver = sc[1].text
if all == False:
if id.isupper() or '-MIB' in id or 'SNMP' in id:
continue
schema.set('name', id)
if ver is None:
schema.set('version', '')
else:
schema.set('version', ver)
unmatched = validate_schema(request.user.username, id, ver)
if unmatched is not None:
schema.set('unmatched', unmatched)
schemas.append(schema)
return HttpResponse(Response.success('get', 'ok', xml=schemas))
开发者ID:center3d,项目名称:yang-explorer,代码行数:43,代码来源:schema.py
示例14: upload_handler
def upload_handler(request):
""" HTTP Request handler function to upload yang models """
if not request.user.is_authenticated():
logging.debug('Unauthorized upload request .. ')
return HttpResponse(Response.error('upload', 'Unauthorized'))
mode = request.GET.get('mode', '')
logging.debug(request.method + ':Received upload request .. ' + mode)
if request.method == 'POST':
# create a temporary storage for this session
directory = os.path.join('data', 'session', request.session.session_key)
_file = upload_file(request.FILES['Filedata'], directory)
if _file is not None:
module = ET.Element('module')
module.text = _file
rval = Response.success('upload', 'ok', xml=module)
logging.debug(rval)
return HttpResponse(rval)
return HttpResponse(Response.error('upload', 'Failed to upload'))
elif request.method == 'GET':
if mode == 'sync':
filename = request.GET.get('file', '')
index = request.GET.get('index', '')
logging.debug('Received sync request for ' + filename + ', index ' + index)
success, response = sync_file(request.user.username, request.session.session_key,
filename, index)
if success:
return HttpResponse(Response.success(mode, 'ok'))
return HttpResponse(Response.error(mode, 'compilation failed', xml=response))
elif mode == 'commit':
success, modules = commit_files(request.user.username, request.session.session_key)
if success:
return HttpResponse(Response.success('commit', 'ok', modules))
elif mode == 'init':
success, modules = get_upload_files(request.user.username, request.session.session_key)
if success:
return HttpResponse(Response.success(mode, 'ok', modules))
elif mode == 'clear':
success, modules = clear_upload_files(request.user.username, request.session.session_key)
if success:
return HttpResponse(Response.success(mode, 'ok', modules))
return HttpResponse(Response.error(mode, 'failed'))
return render_to_response('upload.html')
开发者ID:janlindblad,项目名称:yang-explorer,代码行数:48,代码来源:views.py
示例15: get_schema
def get_schema(request, req, all=True):
"""
This API get yang schema from device
"""
logger.debug("Get Yang Schema")
req = req.replace("<metadata>", "")
req = req.replace("</metadata>", "")
protocol, device, fmt, _, payload = Adapter.parse_request(req)
if device.get("host", None) is None:
return HttpResponse(Response.error("get", "no host info"))
rpc_xml = ET.fromstring(get_schema_list_rpc)
xml = Adapter.run_netconf(request.user.username, device, rpc_xml)
if xml is None:
return HttpResponse(Response.error("get", "failed to get schema"))
if xml.text is not None:
if xml.text.find("error"):
return HttpResponse(Response.error("get", xml.text))
sclist = xml[0][0][0][0]
schemas = ET.Element("schemas")
for sc in sclist:
schema = ET.Element("schema")
id = sc[0].text
ver = sc[1].text
if all == False:
if id.isupper() or "-MIB" in id or "SNMP" in id:
continue
schema.set("name", id)
if ver is None:
schema.set("version", "")
else:
schema.set("version", ver)
unmatched = validate_schema(request.user.username, id, ver)
if unmatched is not None:
schema.set("unmatched", unmatched)
schemas.append(schema)
return HttpResponse(Response.success("get", "ok", xml=schemas))
开发者ID:CiscoDevNet,项目名称:yang-explorer,代码行数:43,代码来源:schema.py
示例16: search_handler
def search_handler(request):
"""
Args:
request: Django HTTP request header
Returns: HTTP response with search results
"""
if not request.user.is_authenticated():
return HttpResponse(Response.error(None, 'User must be logged in'))
query = request.GET.get('query', '')
mode = request.GET.get('mode', '')
if not query:
rc, result = False, 'Invalid or empty query'
else:
rc, result = Search.search(request.user.username, query)
if not rc:
return HttpResponse(Response.error(mode, result))
return HttpResponse(Response.success(mode, 'ok', xml=result))
开发者ID:danhut,项目名称:yang-explorer,代码行数:21,代码来源:views.py
示例17: request_handler
def request_handler(request):
""" HTTP Request handler function to handle actions on collections """
mode = request.GET.get('mode', '')
reply_xml = None
logging.debug('request_handler: Received Request: (%s)' % mode)
if mode == 'get-collection-list':
reply_xml = Collection.list()
elif mode == 'load-collection':
metadata = request.GET.get('metadata', '')
reply_xml = Collection.load(metadata)
if reply_xml is None:
return HttpResponse(Response.error(mode, 'Failed'))
elif mode == 'add-collection':
metadata = request.GET.get('metadata', '')
payload = request.GET.get('payload', '')
if not Collection.add(metadata, payload):
return HttpResponse(Response.error(mode, 'Failed'))
reply_xml = Collection.list()
mode = 'get-collection-list'
elif mode == 'delete-collection':
metadata = request.GET.get('metadata', '')
if not Collection.remove(metadata):
return HttpResponse(Response.error(mode, 'Failed'))
reply_xml = Collection.list()
mode = 'get-collection-list'
elif mode in ['get-cap', 'run-rpc']:
payload = request.GET.get('payload', '')
reply_xml = Adapter.run_request(payload)
return HttpResponse(Response.success(mode, 'ok', reply_xml))
开发者ID:janlindblad,项目名称:yang-explorer,代码行数:40,代码来源:views.py
示例18: login_handler
def login_handler(request):
""" HTTP Request handler function for user login / logout requests """
session = ET.Element('session')
if request.POST:
action = request.POST['action']
if action == 'login':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
# Correct password, and the user is marked "active"
login(request, user)
session.text = username
else:
return HttpResponse(Response.error('login', 'Authentication Failed'))
else:
try:
logout(request)
except Exception as ex:
print(ex.__doc__)
print(ex.message)
return HttpResponse(Response.success(action, 'ok', session))
return HttpResponse(Response.error(action, 'Invalid Request'))
开发者ID:pgohite,项目名称:yang-explorer,代码行数:23,代码来源:views.py
示例19: admin_handler
def admin_handler(request):
""" HTTP Request handler function to handle actions on yang modules """
if not request.user.is_authenticated():
return HttpResponse(Response.error(None, 'User must be logged in'))
if request.method != 'GET':
return HttpResponse(Response.error(None, 'Invalid admin Request'))
action = request.GET.get('action', '')
logger.info('Received admin request %s for user %s' % (action, request.user.username))
if action in ['subscribe', 'unsubscribe', 'delete', 'graph']:
payload = request.GET.get('payload', None)
print(str(payload))
(rc, msg) = ModuleAdmin.admin_action(request.user.username, payload, action)
if not rc:
return HttpResponse(Response.error(action, msg))
if action == 'graph':
return HttpResponse(Response.success(action, msg))
modules = ModuleAdmin.get_modules(request.user.username)
return HttpResponse(Response.success(action, 'ok', xml=modules))
开发者ID:sachinsr099,项目名称:yang-explorer,代码行数:24,代码来源:views.py
示例20: profile_handler
def profile_handler(request):
""" HTTP request handler for profile request """
profiles = ET.Element('profiles')
if request.user.is_authenticated():
uid = request.user.id
logging.debug("User Authenticated (%s)" % request.user.username)
entries = DeviceProfile.objects.filter(Q(user=uid) | Q(shared=True))
for e in entries:
profile = _build_device_profile(e)
profiles.append(profile)
entries = Collection.objects.all()
for e in entries:
profile = _build_collection_profile(e)
profiles.append(profile)
return HttpResponse(Response.success('profile', 'ok', xml=profiles))
开发者ID:MFALHI,项目名称:yang-explorer,代码行数:17,代码来源:profile_view.py
注:本文中的explorer.utils.misc.Response类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论