Created
June 27, 2013 11:21
-
-
Save engalar/5875720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ftplib import FTP | |
from optparse import OptionParser | |
import sys | |
class ETP(FTP): | |
def set_result_type(self, result_type): | |
# 0: default | |
# 1: ftp link | |
# 2: UNC format | |
self.result_type = result_type | |
def query(self, term, max_num=0xffffffff, match_case=False, match_whole_word=False, match_path=False): | |
qs = "QUERY 0 %d %d %d %d %s" % (max_num, match_case, match_whole_word, match_path, term) | |
if self.debugging: print '*QUERY*', self.sanitize(qs) | |
resp = self.sendcmd(qs) | |
code, offset, count, numfolders, numfiles = resp.split() | |
if self.debugging: print code, offset, count, numfolders, numfiles | |
result = [] | |
for i in range(int(count)): | |
line = self.getmultiline().decode('utf-8') | |
result.append(self.convert_result(line)) | |
return (int(numfolders), int(numfiles), result) | |
def convert_result(self, line): | |
if self.result_type == 1: | |
return 'ftp://%s:%d/%s' % (self.host, self.port, line.replace('\\', '/')) | |
if self.result_type == 2: | |
return '\\\\%s\\%c$\\%s' % (self.host, line[0], line[3:]) | |
return line | |
if __name__ == '__main__': | |
parser = OptionParser() | |
parser.add_option("-s", "", type="string", dest="server", | |
help="ETP server name or address") | |
parser.add_option("-t", "", type="int", dest="port", default=21, | |
help="ETP server port") | |
parser.add_option("", "--user", type="string", dest="user") | |
parser.add_option("", "--pwd", type="string", dest="pwd") | |
parser.add_option("-n", "", type="int", dest="max_num", default=100, | |
help="Limit the amount of results shown") | |
parser.add_option("-i", "", action="store_true", dest="match_case", default=False, | |
help="Match case") | |
parser.add_option("-w", "", action="store_true", dest="match_whole_word", default=False, | |
help="Match whole word") | |
parser.add_option("-p", "", action="store_true", dest="match_path", default=False, | |
help="Match path") | |
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, | |
help="Print verbose and debug information") | |
parser.add_option("-l", "", type="int", dest="result_type", default=2, | |
help="Change result type: 0: use the result as-is, 1: use ftp link, 2: use UNC format") | |
options, args = parser.parse_args() | |
if not args: | |
print >>sys.stderr, 'Invalid query' | |
sys.exit(1) | |
etp = ETP() | |
if options.verbose: etp.set_debuglevel(2) | |
etp.set_result_type(options.result_type) | |
etp.connect(options.server, options.port) | |
etp.login(options.user, options.pwd) | |
numfolders, numfiles, results = etp.query(' '.join(args), options.max_num, options.match_case, options.match_whole_word, options.match_path) | |
print '\n'.join(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment