def exec_downloadexecute(url):
try:
r = requests.get(url)
filename = url.split('/')[-1]
if r.status_code == 200:
f = open(filename,'wb')
f.write(r.content)
f.close()
executeBackground(filename)
data = "[+] Task Completed Successfully [+]"
else:
data = "[-] Error [-]"
except Exception, err:
data = err
return base64.b64encode(str(data))
python类split()的实例源码
def wrap_onspace(self, text, width):
return reduce(lambda line, word, width=width: '%s%s%s' % (
line,
' ' if len(line.rsplit('\n')[-1]+word.split('\n')[0])>=width else '\n',
word), text.split(' '))
def getAsRows(self, data):
return [row.strip().split(',') for row in data.splitlines()]
def __init__(self, prompt, banner=None):
self.prompt = prompt
self.banner = banner
self.commands = {}
self.commandSort = []
self.db = None
for i in dir(self):
if "cmd_" == i[:4]:
cmd = i.split("cmd_")[1].lower()
self.commands[cmd] = getattr(self, i)
try:
self.commandSort.append((int(self
.commands[cmd].__doc__.split(
"|")[0]), cmd))
except:
pass
self.commandSort.sort()
self.commandSort = [i[1] for i in self.commandSort]
self.var_DEBUG = False
self.var_tableStyle = ''
self.configvars = {}
for i in dir(self):
if "var_" == i[:4]:
var = i.split("var_")[1]
self.configvars[var] = i
def execute(self, cmd):
try:
if not '-table ' in cmd:
exec cmd
else:
file = None
table = None
fields = []
items = cmd.split()
invalidParams = []
table = self.getTable(items[1])
allowedParams = ['fields', 'file']
for i in items:
if '=' in i and not i.split('=')[0] in allowedParams:
try:
invalidParams.append(i)
except Exception, err:
raise Exception('invalid parameter\n%s' % i)
else:
if 'file=' in i:
file = os.path.abspath(i.split('=')[0].strip())
if 'fields=' in i:
for field in i.split('=')[1].split():
if field in self.db[table].fields:
fields.append(field.strip())
if len(invalidParams) > 0:
print('the following parameter(s) is not valid\n%s' %
','.join(invalidParams))
else:
try:
self.cmd_table(table, file, fields)
except Exception, err:
print('could not generate table for table %s\n%s' % (table, err))
except Exception, err:
print('sorry, can not do that!\n%s' % err)
def cmd_help(self, *args):
'''-3|help|Show's help'''
alldata = []
lengths = []
for i in self.commandSort:
alldata.append(
self.commands[i].__doc__.split("|")[1:])
for i in alldata:
if len(i) > len(lengths):
for j in range(len(i)
- len(lengths)):
lengths.append(0)
j = 0
while j < len(i):
if len(i[j]) > lengths[j]:
lengths[j] = len(i[j])
j += 1
print ("-" * (lengths[0] + lengths[1] + 4))
for i in alldata:
print (("%-" + str(lengths[0]) + "s - %-" + str(
lengths[1]) + "s") % (i[0], i[1]))
if len(i) > 2:
for j in i[2:]: print (("%" + str(lengths[
0] + 9) + "s* %s") % (" ", j))
print
def wrap_onspace(self, text, width):
return reduce(lambda line, word, width=width: '%s%s%s' % (
line,
' ' if len(line.rsplit('\n')[-1]+word.split('\n')[0])>=width else '\n',
word), text.split(' '))
def getAsRows(self, data):
return [row.strip().split(',') for row in data.splitlines()]
def __init__(self, prompt, banner=None):
self.prompt = prompt
self.banner = banner
self.commands = {}
self.commandSort = []
self.db = None
for i in dir(self):
if "cmd_" == i[:4]:
cmd = i.split("cmd_")[1].lower()
self.commands[cmd] = getattr(self, i)
try:
self.commandSort.append((int(self
.commands[cmd].__doc__.split(
"|")[0]), cmd))
except:
pass
self.commandSort.sort()
self.commandSort = [i[1] for i in self.commandSort]
self.var_DEBUG = False
self.var_tableStyle = ''
self.configvars = {}
for i in dir(self):
if "var_" == i[:4]:
var = i.split("var_")[1]
self.configvars[var] = i
def execute(self, cmd):
try:
if not '-table ' in cmd:
exec cmd
else:
file = None
table = None
fields = []
items = cmd.split()
invalidParams = []
table = self.getTable(items[1])
allowedParams = ['fields', 'file']
for i in items:
if '=' in i and not i.split('=')[0] in allowedParams:
try:
invalidParams.append(i)
except Exception, err:
raise Exception('invalid parameter\n%s' % i)
else:
if 'file=' in i:
file = os.path.abspath(i.split('=')[0].strip())
if 'fields=' in i:
for field in i.split('=')[1].split():
if field in self.db[table].fields:
fields.append(field.strip())
if len(invalidParams) > 0:
print('the following parameter(s) is not valid\n%s' %
','.join(invalidParams))
else:
try:
self.cmd_table(table, file, fields)
except Exception, err:
print('could not generate table for table %s\n%s' % (table, err))
except Exception, err:
print('sorry, can not do that!\n%s' % err)
def cmd_help(self, *args):
'''-3|help|Show's help'''
alldata = []
lengths = []
for i in self.commandSort:
alldata.append(
self.commands[i].__doc__.split("|")[1:])
for i in alldata:
if len(i) > len(lengths):
for j in range(len(i)
- len(lengths)):
lengths.append(0)
j = 0
while j < len(i):
if len(i[j]) > lengths[j]:
lengths[j] = len(i[j])
j += 1
print ("-" * (lengths[0] + lengths[1] + 4))
for i in alldata:
print (("%-" + str(lengths[0]) + "s - %-" + str(
lengths[1]) + "s") % (i[0], i[1]))
if len(i) > 2:
for j in i[2:]: print (("%" + str(lengths[
0] + 9) + "s* %s") % (" ", j))
print
def wrap_onspace(self, text, width):
return reduce(lambda line, word, width=width: '%s%s%s' % (
line,
' ' if len(line.rsplit('\n')[-1]+word.split('\n')[0])>=width else '\n',
word), text.split(' '))
def getAsRows(self, data):
return [row.strip().split(',') for row in data.splitlines()]
def __init__(self, prompt, banner=None):
self.prompt = prompt
self.banner = banner
self.commands = {}
self.commandSort = []
self.db = None
for i in dir(self):
if "cmd_" == i[:4]:
cmd = i.split("cmd_")[1].lower()
self.commands[cmd] = getattr(self, i)
try:
self.commandSort.append((int(self
.commands[cmd].__doc__.split(
"|")[0]), cmd))
except:
pass
self.commandSort.sort()
self.commandSort = [i[1] for i in self.commandSort]
self.var_DEBUG = False
self.var_tableStyle = ''
self.configvars = {}
for i in dir(self):
if "var_" == i[:4]:
var = i.split("var_")[1]
self.configvars[var] = i
def execute(self, cmd):
try:
if not '-table ' in cmd:
exec cmd
else:
file = None
table = None
fields = []
items = cmd.split()
invalidParams = []
table = self.getTable(items[1])
allowedParams = ['fields', 'file']
for i in items:
if '=' in i and not i.split('=')[0] in allowedParams:
try:
invalidParams.append(i)
except Exception, err:
raise Exception('invalid parameter\n%s' % i)
else:
if 'file=' in i:
file = os.path.abspath(i.split('=')[0].strip())
if 'fields=' in i:
for field in i.split('=')[1].split():
if field in self.db[table].fields:
fields.append(field.strip())
if len(invalidParams) > 0:
print('the following parameter(s) is not valid\n%s' %
','.join(invalidParams))
else:
try:
self.cmd_table(table, file, fields)
except Exception, err:
print('could not generate table for table %s\n%s' % (table, err))
except Exception, err:
print('sorry, can not do that!\n%s' % err)
def cmd_help(self, *args):
'''-3|help|Show's help'''
alldata = []
lengths = []
for i in self.commandSort:
alldata.append(
self.commands[i].__doc__.split("|")[1:])
for i in alldata:
if len(i) > len(lengths):
for j in range(len(i)
- len(lengths)):
lengths.append(0)
j = 0
while j < len(i):
if len(i[j]) > lengths[j]:
lengths[j] = len(i[j])
j += 1
print ("-" * (lengths[0] + lengths[1] + 4))
for i in alldata:
print (("%-" + str(lengths[0]) + "s - %-" + str(
lengths[1]) + "s") % (i[0], i[1]))
if len(i) > 2:
for j in i[2:]: print (("%" + str(lengths[
0] + 9) + "s* %s") % (" ", j))
print
def wrap_onspace(self, text, width):
return reduce(lambda line, word, width=width: '%s%s%s' % (
line,
' ' if len(line.rsplit('\n')[-1]+word.split('\n')[0])>=width else '\n',
word), text.split(' '))
def getAsRows(self, data):
return [row.strip().split(',') for row in data.splitlines()]
def __init__(self, prompt, banner=None):
self.prompt = prompt
self.banner = banner
self.commands = {}
self.commandSort = []
self.db = None
for i in dir(self):
if "cmd_" == i[:4]:
cmd = i.split("cmd_")[1].lower()
self.commands[cmd] = getattr(self, i)
try:
self.commandSort.append((int(self
.commands[cmd].__doc__.split(
"|")[0]), cmd))
except:
pass
self.commandSort.sort()
self.commandSort = [i[1] for i in self.commandSort]
self.var_DEBUG = False
self.var_tableStyle = ''
self.configvars = {}
for i in dir(self):
if "var_" == i[:4]:
var = i.split("var_")[1]
self.configvars[var] = i
def execute(self, cmd):
try:
if not '-table ' in cmd:
exec cmd
else:
file = None
table = None
fields = []
items = cmd.split()
invalidParams = []
table = self.getTable(items[1])
allowedParams = ['fields', 'file']
for i in items:
if '=' in i and not i.split('=')[0] in allowedParams:
try:
invalidParams.append(i)
except Exception, err:
raise Exception('invalid parameter\n%s' % i)
else:
if 'file=' in i:
file = os.path.abspath(i.split('=')[0].strip())
if 'fields=' in i:
for field in i.split('=')[1].split():
if field in self.db[table].fields:
fields.append(field.strip())
if len(invalidParams) > 0:
print('the following parameter(s) is not valid\n%s' %
','.join(invalidParams))
else:
try:
self.cmd_table(table, file, fields)
except Exception, err:
print('could not generate table for table %s\n%s' % (table, err))
except Exception, err:
print('sorry, can not do that!\n%s' % err)
def cmd_help(self, *args):
'''-3|help|Show's help'''
alldata = []
lengths = []
for i in self.commandSort:
alldata.append(
self.commands[i].__doc__.split("|")[1:])
for i in alldata:
if len(i) > len(lengths):
for j in range(len(i)
- len(lengths)):
lengths.append(0)
j = 0
while j < len(i):
if len(i[j]) > lengths[j]:
lengths[j] = len(i[j])
j += 1
print ("-" * (lengths[0] + lengths[1] + 4))
for i in alldata:
print (("%-" + str(lengths[0]) + "s - %-" + str(
lengths[1]) + "s") % (i[0], i[1]))
if len(i) > 2:
for j in i[2:]: print (("%" + str(lengths[
0] + 9) + "s* %s") % (" ", j))
print
def wrap_onspace(self, text, width):
return reduce(lambda line, word, width=width: '%s%s%s' % (
line,
' ' if len(line.rsplit('\n')[-1]+word.split('\n')[0])>=width else '\n',
word), text.split(' '))
def getAsRows(self, data):
return [row.strip().split(',') for row in data.splitlines()]
def __init__(self, prompt, banner=None):
self.prompt = prompt
self.banner = banner
self.commands = {}
self.commandSort = []
self.db = None
for i in dir(self):
if "cmd_" == i[:4]:
cmd = i.split("cmd_")[1].lower()
self.commands[cmd] = getattr(self, i)
try:
self.commandSort.append((int(self
.commands[cmd].__doc__.split(
"|")[0]), cmd))
except:
pass
self.commandSort.sort()
self.commandSort = [i[1] for i in self.commandSort]
self.var_DEBUG = False
self.var_tableStyle = ''
self.configvars = {}
for i in dir(self):
if "var_" == i[:4]:
var = i.split("var_")[1]
self.configvars[var] = i
def execute(self, cmd):
try:
if not '-table ' in cmd:
exec cmd
else:
file = None
table = None
fields = []
items = cmd.split()
invalidParams = []
table = self.getTable(items[1])
allowedParams = ['fields', 'file']
for i in items:
if '=' in i and not i.split('=')[0] in allowedParams:
try:
invalidParams.append(i)
except Exception, err:
raise Exception('invalid parameter\n%s' % i)
else:
if 'file=' in i:
file = os.path.abspath(i.split('=')[0].strip())
if 'fields=' in i:
for field in i.split('=')[1].split():
if field in self.db[table].fields:
fields.append(field.strip())
if len(invalidParams) > 0:
print('the following parameter(s) is not valid\n%s' %
','.join(invalidParams))
else:
try:
self.cmd_table(table, file, fields)
except Exception, err:
print('could not generate table for table %s\n%s' % (table, err))
except Exception, err:
print('sorry, can not do that!\n%s' % err)
def cmd_help(self, *args):
'''-3|help|Show's help'''
alldata = []
lengths = []
for i in self.commandSort:
alldata.append(
self.commands[i].__doc__.split("|")[1:])
for i in alldata:
if len(i) > len(lengths):
for j in range(len(i)
- len(lengths)):
lengths.append(0)
j = 0
while j < len(i):
if len(i[j]) > lengths[j]:
lengths[j] = len(i[j])
j += 1
print ("-" * (lengths[0] + lengths[1] + 4))
for i in alldata:
print (("%-" + str(lengths[0]) + "s - %-" + str(
lengths[1]) + "s") % (i[0], i[1]))
if len(i) > 2:
for j in i[2:]: print (("%" + str(lengths[
0] + 9) + "s* %s") % (" ", j))
print
def executeBackground(command):
subprocess.Popen([command.split()])
return True
def exec_cmd(cmd):
data = ExecuteShellCommand(cmd.split())
return base64.b64encode(str(data))
def wrapTable(
self, rows, hasHeader=False, headerChar='-', delim=' | ', justify='left',
separateRows=False, prefix='', postfix='', wrapfunc=lambda x: x):
def rowWrapper(row):
'''---
newRows is returned like
[['w'], ['x'], ['y'], ['z']]
---'''
newRows = [wrapfunc(item).split('\n') for item in row]
self.rows = newRows
'''---
rowList gives like newRows but
formatted like [[w, x, y, z]]
---'''
rowList = [[substr or '' for substr in item]
for item in map(None, *newRows)]
return rowList
logicalRows = [rowWrapper(row) for row in rows]
columns = map(None, *reduce(operator.add, logicalRows))
self.columns = columns
maxWidths = [max(
[len(str
(item)) for
item in column]
) for column
in columns]
rowSeparator = headerChar * (len(prefix) +len(postfix) + sum(maxWidths) +
len(delim) * (len(maxWidths) - 1))
justify = {'center': str.center,
'right': str.rjust,
'left': str.ljust
}[justify.lower()]
output = cStringIO.StringIO()
if separateRows:
print >> output, rowSeparator
for physicalRows in logicalRows:
for row in physicalRows:
print >> output,\
prefix + delim.join([
justify(str(item), width) for (
item, width) in zip(row, maxWidths)]
) + postfix
if separateRows or hasHeader:
print >> output, rowSeparator
hasHeader = False
return output.getvalue()
def wrapTable(
self, rows, hasHeader=False, headerChar='-', delim=' | ', justify='left',
separateRows=False, prefix='', postfix='', wrapfunc=lambda x: x):
def rowWrapper(row):
'''---
newRows is returned like
[['w'], ['x'], ['y'], ['z']]
---'''
newRows = [wrapfunc(item).split('\n') for item in row]
self.rows = newRows
'''---
rowList gives like newRows but
formatted like [[w, x, y, z]]
---'''
rowList = [[substr or '' for substr in item]
for item in map(None, *newRows)]
return rowList
logicalRows = [rowWrapper(row) for row in rows]
columns = map(None, *reduce(operator.add, logicalRows))
self.columns = columns
maxWidths = [max(
[len(str
(item)) for
item in column]
) for column
in columns]
rowSeparator = headerChar * (len(prefix) +len(postfix) + sum(maxWidths) +
len(delim) * (len(maxWidths) - 1))
justify = {'center': str.center,
'right': str.rjust,
'left': str.ljust
}[justify.lower()]
output = cStringIO.StringIO()
if separateRows:
print >> output, rowSeparator
for physicalRows in logicalRows:
for row in physicalRows:
print >> output,\
prefix + delim.join([
justify(str(item), width) for (
item, width) in zip(row, maxWidths)]
) + postfix
if separateRows or hasHeader:
print >> output, rowSeparator
hasHeader = False
return output.getvalue()