ftp代码优化

This commit is contained in:
midoks
2022-07-02 13:12:32 +08:00
parent 32fdbe7ca4
commit da6f66866b
+51 -69
View File
@@ -59,6 +59,13 @@ def getArgs():
return tmp return tmp
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
def status(): def status():
cmd = "ps -ef|grep pure-ftpd |grep -v grep | grep -v python | awk '{print $2}'" cmd = "ps -ef|grep pure-ftpd |grep -v grep | grep -v python | awk '{print $2}'"
data = mw.execShell(cmd) data = mw.execShell(cmd)
@@ -122,72 +129,61 @@ def initDreplace():
return file_bin return file_bin
def start(): def pfOp(method):
file = initDreplace() file = initDreplace()
data = mw.execShell(file + ' start')
if not mw.isAppleSystem():
data = mw.execShell('systemctl ' + method + ' pureftp')
if data[1] == '':
return 'ok'
return 'fail'
data = mw.execShell(file + ' ' + method)
if data[1] == '': if data[1] == '':
return 'ok' return 'ok'
return data[1] return data[1]
def start():
return pfOp('start')
def stop(): def stop():
file = initDreplace() return pfOp('stop')
data = mw.execShell(file + ' stop')
if data[1] == '':
return 'ok'
return data[1]
def restart(): def restart():
file = initDreplace() return pfOp('restart')
data = mw.execShell(file + ' restart')
if data[1] == '':
return 'ok'
return 'fail'
def reload(): def reload():
file = initDreplace() return pfOp('reload')
data = mw.execShell(file + ' reload')
if data[1] == '':
return 'ok'
return data[1]
def initdStatus(): def initdStatus():
if not app_debug: if mw.isAppleSystem():
os_name = mw.getOs() return "Apple Computer does not support"
if os_name == 'darwin':
return "Apple Computer does not support" shell_cmd = 'systemctl status pureftp | grep loaded | grep "enabled;"'
initd_bin = getInitDFile() data = mw.execShell(shell_cmd)
if os.path.exists(initd_bin): if data[0] == '':
return 'ok' return 'fail'
return 'fail' return 'ok'
def initdInstall(): def initdInstall():
import shutil if mw.isAppleSystem():
if not app_debug: return "Apple Computer does not support"
os_name = mw.getOs()
if os_name == 'darwin':
return "Apple Computer does not support"
source_bin = initDreplace() mw.execShell('systemctl start pureftp')
initd_bin = getInitDFile()
shutil.copyfile(source_bin, initd_bin)
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
return 'ok' return 'ok'
def initdUinstall(): def initdUinstall():
if not app_debug: if mw.isAppleSystem():
os_name = mw.getOs() return "Apple Computer does not support"
if os_name == 'darwin':
return "Apple Computer does not support" mw.execShell('systemctl stop pureftp')
initd_bin = getInitDFile()
os.remove(initd_bin)
mw.execShell('chkconfig --del ' + getPluginName())
return 'ok' return 'ok'
@@ -245,8 +241,9 @@ def pftpStart(username):
def pftpReload(): def pftpReload():
mw.execShell(getServerDir() + '/bin/pure-pw mkdb ' + cmd = getServerDir() + '/bin/pure-pw mkdb ' +
getServerDir() + '/etc/pureftpd.pdb') getServerDir() + '/etc/pureftpd.pdb'
mw.execShell(cmd)
def getWwwDir(): def getWwwDir():
@@ -311,19 +308,11 @@ def getFtpList():
def addFtp(): def addFtp():
import urllib import urllib
args = getArgs() args = getArgs()
if not 'ftp_username' in args: data = checkArgs(args, ['ftp_username', 'ftp_password', 'path', 'ps'])
return 'ftp_username missing' if not data[0]:
return data[1]
if not 'ftp_password' in args: path = urllib.request.unquote(args['path'])
return 'ftp_password missing'
if not 'path' in args:
return 'path missing'
if not 'ps' in args:
return 'ps missing'
path = urllib.unquote(args['path'])
user = args['ftp_username'] user = args['ftp_username']
pwd = args['ftp_password'] pwd = args['ftp_password']
ps = args['ps'] ps = args['ps']
@@ -342,11 +331,9 @@ def addFtp():
def delFtp(): def delFtp():
args = getArgs() args = getArgs()
if not 'id' in args: data = checkArgs(args, ['id', 'username'])
return 'ftp_username missing' if not data[0]:
return data[1]
if not 'username' in args:
return 'username missing'
mw.execShell(getServerDir() + mw.execShell(getServerDir() +
'/bin/pure-pw userdel ' + args['username']) '/bin/pure-pw userdel ' + args['username'])
@@ -359,14 +346,9 @@ def delFtp():
def modFtp(): def modFtp():
args = getArgs() args = getArgs()
if not 'id' in args: data = checkArgs(args, ['id', 'name', 'password'])
return 'id missing' if not data[0]:
return data[1]
if not 'name' in args:
return 'name missing'
if not 'password' in args:
return 'password missing'
conn = pftpDB() conn = pftpDB()
data = pftpMod(args['name'], args['password']) data = pftpMod(args['name'], args['password'])