Advanced Python FTP - can I control how ftplib talks to a server?

I need to send a very specific (non-standard) string to an FTP server:

dir "SYS:\IC.ICAMA."

The case is critical, as are the style of quotes and their content.

Unfortunately, ftplib.dir() seems to use the 'LIST' command rather than 'dir' (and it uses the wrong case for this application).

The FTP server is actually a telephone switch and it's a very non-standard implementation.

I tried using ftplib.sendcmd(), but it also sends 'pasv' as part of the command sequence.

Is there an easy way of issuing specific commands to an FTP server?


Asked by: Alberta831 | Posted: 28-01-2022






Answer 1

Try the following. It is a modification of the original FTP.dir command which uses "dir" instead of "LIST". It gives a "DIR not understood" error with the ftp server I tested it on, but it does send the command you're after. (You will want to remove the print command I used to check that.)

import ftplib

class FTP(ftplib.FTP):

    def shim_dir(self, *args):
        '''List a directory in long form.
        By default list current directory to stdout.
        Optional last argument is callback function; all
        non-empty arguments before it are concatenated to the
        LIST command.  (This *should* only be used for a pathname.)'''
        cmd = 'dir'
        func = None
        if args[-1:] and type(args[-1]) != type(''):
            args, func = args[:-1], args[-1]
        for arg in args:
            if arg:
                cmd = cmd + (' ' + arg)
        print cmd
        self.retrlines(cmd, func)

if __name__ == '__main__':
    f = FTP('ftp.ncbi.nih.gov')
    f.login()
    f.shim_dir('"blast"')

Answered by: Lenny243 | Posted: 01-03-2022



Similar questions

.net - Advanced SAX Parser in C#

See Below is the XML Arch. I want to display it in row / column wize. What I need is I need to convert this xml file to Hashtable like, {"form" : {"attrs" : { "string" : " Partners" } {"child1": { "group" : { "attrs" : { "col" : "6", "colspan":"1" } } { "child1": { "field" : { "attrs" : { "name":"name"} { "child2": { "field...


python - Image library with advanced text effects?

What would be the best Python library to use when producing text-based images requiring things such as leading, kerning, outlines, drop-shadows, etc? I've worked with PIL before for resizing images, but the methods for working with text seem rather limited. Is there a better alternative?


python - Is it possible in numpy to use advanced list slicing and still get a view?

In other words, I want to do something like A[[-1, 0, 1], [2, 3, 4]] += np.ones((3, 3)) instead of A[-1:3, 2:5] += np.ones((1, 3)) A[0:2, 2:5] += np.ones((2, 3))


python - Add advanced features to a tkinter Text widget

I am working on a simple messaging system, and need to add the following to a Tkinter text widget: Spell Check Option To Change Font ( on selected text ) Option to change font color ( on selected text ) Option to Change Font Size ( on selected text ) I understand that the tkinter Text widget has the ability to use multiple fonts and colors through the tagging mechani...


python - Advanced sorting criteria for a list of nested tuples

I have a list of nested tuples of the form: [(a, (b, c)), ...] Now I would like to pick the element which maximizes a while minimizing b and c at the same time. For example in [(7, (5, 1)), (7, (4, 1)), (6, (3, 1))] the winner should be (7, (4, 1)) Any help is appreciated.


Python help Advanced Objects: Special Methods

I am hoping someone can give me some advice on my code? I am learning python via a self study course and one of the assgnments requirements is as below: create a program named centipede.py, including a class named "Centipede." This class has the following requirements: Once instantiated if called with a value, it appends that argument to an internal list: >>> from centipede...


python - Make Advanced Searches in SQLite Data Base

I got and address book with names of people. I use the following code to perform a search in the name column: register = [] text = "{0}%" .format(self.lineEdit_6.text()) c.execute("select id, name from contacts where nome like '{0}'" .format(text)) for row in c: register.append(row) self.listWidget_4.addItem(str(row[1])) self.listWidget_6.addItem(str(row[0])) if register == []: self.listWidg...


Setting up advanced Python logging

I'd like to use logging for my modules, but I'm not sure how to design the following requirements: normal logging levels (info, error, warning, debug) but also some additional more verbose debug levels logging messages can have different types; some are meant for the developer, some are meant for the user; those types go to different outputs errors should go to stderr I...


python - Numpy Advanced Slicing

I am trying to slice the below array to get rows 2 and 3 and the first column in addition to the columns between the 2nd and last columns, but every slice I have tried does not seem to work. For example, the first print statement below gives a syntax error because of the : in the brackets. I have also tried to simply concatenate the arrays, but I don't think this is the most efficient way to accomplish this problem.


sql - Python Sqlite3 advanced grouping

I hope this question has not been asked before. I have a csv file containing below columns and info and I can upload this to sqlite3 database with the exact same column names Company Type Qty Price ABC r 5 $$$ ABC h 9 $$$ ABC s 10 $$$ DER a 3 $$$ DER z 6 $$$ GGG q 12 $$$ GGG w 20 $$$ but, how do I get the below result on a ...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top