optparse is a more convenient, flexible, and powerful library for parsing command-line options than
the old getopt module. optparse uses a more declarative style of command-line parsing: you create
an instance of OptionParser, populate it with options, and parse the command line. optparse
allows users to specify options in the conventional GNU/POSIX syntax, and additionally generates
usage and help messages for you.
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
(options, args) = parser.parse_args()
执行脚本,获取帮助信息
[root@python script]# python 04_optparse.py -h
Usage: 04_optparse.py [options]
Options:
-h, --help show this help message and exit
-f FILE, --file=FILE write report to FILE
-q, --quiet don't print status messages to stdout
[root@python script]# python 07_optparse.py -h
Usage: 07_optparse.py [options]
Options:
-h, --help show this help message and exit
-l LOCAL, --local=LOCAL
local file or directory</pre><pre class="brush:python;toolbar:false">[root@python script]# python 07_optparse.py -l nihao
[root@python script]# python 07_optparse.py -h
Usage: 07_optparse.py [options]
Options:
-h, --help show this help message and exit
-l, --local local file or directory
#!/usr/bin/env pythoncoding:utf8
from multiprocessing import Process
from optparse import OptionParser
import paramiko
import sys
import os
Username = 'root'
Password = 'redhat'
Port = 22
def opt():
parser = OptionParser()
parser.add_option('-l','--local',
dest='local',
action='store',
help="local directory's file")
parser.add_option('-r','--remote',
dest='remote',
action='store',
help="remote directory's file")
options, args = parser.parse_args()
return options, args
def fdir(ff):
fileList = []
for p, d, f in os.walk(ff):
files = f
break
for i in files:
ii = os.path.join(ff,i)
fileList.append(ii)
return fileList
def delgen(path):
try:
s = paramiko.Transport((ip,Port))
s.connect(username=Username,password=Password)
sftp = paramiko.SFTPClient.from_transport(s)
sftp.put(localDir,rfile)
s.close()
print '%s put successful.' % ip
except:
print '%s not exists.' % ip
def sftpPuts(ip,localDir,remoteDir):
try:
s = paramiko.Transport((ip,Port))
s.connect(username=Username,password=Password)
sftp = paramiko.SFTPClient.from_transport(s)
for localFile in localDir:
filebasename = os.path.basename(localFile)
remoteFile = '%s/%s' % (remoteDir,filebasename)
sftp.put(localFile,remoteFile)
s.close()
print '%s put successful.' % ip
except:
print '%s not exists.' % ip
def ipProcess01(localFile,remoteFile):
for i in range(2,255):
ip = '192.168.0.%s' % i
p = Process(target=sftpPuts,args=(ip,localFile,remoteFile))
p.start()
def ipProcess02(localDir,rfile):
for i in range(2,255):
ip = '192.168.0.%s' % i
p = Process(target=sftpPut,args=(ip,localDir,rfile))
p.start()
[root@python script]# python 01_optparse_process.py -h
Usage: 01_optparse_process.py [options]
Options:
-h, --help show this help message and exit
-l LOCAL, --local=LOCAL
local directory's file