[Sharing] python script to search ETP

Plug-in and third party software discussion.
Post Reply
wangzq
Posts: 1
Joined: Sun Oct 10, 2010 2:05 am

[Sharing] python script to search ETP

Post by wangzq »

I wanted to search multiple Everything servers at the same time so I created a python script to search using ETP protocol:

Code: Select all

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) 
Then I created following batch file to search both local computer using the ES.exe from Everything and remote computers using this python script:

Code: Select all

@echo off
es.exe %*
python etpclient.py -s 192.168.1.56 -t 210 %*
REM more computers running Everything ETP server ...
Hope someone might find this useful.
l.prieur
Posts: 1
Joined: Tue Dec 07, 2010 12:49 pm

Re: [Sharing] python script to search ETP

Post by l.prieur »

Hello,
I'm interested in your script, but I don't really know Python.
How could i use it? I've got to compile it in Python?

And I'd like to autoconnect to an ETP server, is it the aim of your script?
And with UNC link, we're only able to have access to files in "read only mode".
Could it be possible with your script, to open with a mapped drive link?
Exemple :
UNC is : \\sever1\folder1\foler2\text.txt
Mapped drive would be : \\sever1\folder1\ = L:\
So correct address would be : L:\folder2\text.txt

Is it possible? Do i need to learn Python to do so?

Thanks in advance!
RonnieRocket
Posts: 3
Joined: Thu Jun 30, 2011 3:09 pm

Re: [Sharing] python script to search ETP

Post by RonnieRocket »

first of all: THANK YOU

I notice the sendcmd function that could send something like 'QUERY 0 100 0 0 0 pizza'...

And the list of files is returned.

Can you replicate this in the browser? How would you send that string to an FTP server otherwise?
Like if I were to write this in another language for those that don't have python.. how would I send that?
do you just open a stream and write
'QUERY 0 100 0 0 0 pizza'

or is something more going on? Because I don't get a response
Post Reply