PyTransmit is a simple wrapper on top of the ftplib package which provides an object that can be used to make FTP calls to a PyTransmit installation.
To install PyTransmit from tarball, run the following command:
python setup.py install
You can also download and install PyTransmit directly from the Python Package Index using either pip or easy_install:
pip install PyTransmit easy_install -Z PyTransmit
If you prefer, you can also simply place the pytransmit subdirectory somewhere on your python path. This is useful if you are running the latest version from Mercurial, or want to deploy pytransmit with your application on for example Google App Engine.
Note that you need Python 2.4 or later to use PyTransmit.
This is an example application that handles FTP through PyTransmit:
from pytransmit import FTPClient ftp_obj = FTPClient() # FTP Details FTP_HOST = "" FTP_USER = "" FTP_PASS = "" FTP_PORT = 21 # Connect to the server ftp_obj.connect(FTP_HOST, FTP_USER, FTP_PASS, FTP_PORT) print(ftp_obj.get_message()) # Create a new test directory directory = '/test' ftp_obj.make_directory(directory) print(ftp_obj.get_message()) # Change Directory ftp_obj.change_directory(directory) print(ftp_obj.get_message()) # Get Directory Listing ftp_obj.get_directory_listing()
With the above commands, you can create a FTP connection and create a new /test directory and CD into that directory.
You can easily upload files to the connected server via PyTransmit:
app_root = os.path.dirname(os.path.abspath(__file__)) # Add the file name you want to upload file_path = os.path.join(app_root, 'test.txt') strip_path = file_path.rstrip(os.sep) fp_path = os.path.basename(strip_path) # Upload command ftp_obj.upload_file(fp_path) print(ftp_obj.get_message())
Similarly, you can download the files from the server in the same fashion:
# Add the filename to download ftp_obj.download_file("hello.csv") print(ftp_obj.get_message())
PyTransmit is built on a very small API. The available functions are:
Logs the message to the message_array, from where it is retrieved to display in the console.
Returns the logged message to the console.
|Returns:||Returns the message.|
Connects the remote host to the server from the information provided to the connect method. If the connection is successful, the messaged will logged and displayed in the console, otherwise Exception is raised with the error displayed to the console and program execution halts.
Creates the new directory in the connected server in the root or in the directory specified via the parameter.
|Parameters:||directory – Directory name to create.|
CD’s into the directory of our wish by providing the directory name as the parameter to it.
|Parameters:||directory – Directory name to change to it.|
Checks if the directory you are trying to upload the files is already present or not and if its already present CD’s into the directory and if not, creates the directory and CD’s into the newly created directory.
|Parameters:||directory_name – Directory name to check its existence.|
Lists all the contents in the connected server or in the specified folder in the server.
The file provided with filename will be uploaded to the server in the recommended format automatically to the desired directory.
|Parameters:||filename – Name of the file to upload.|
Downloads the file from the connected server, provided the name is passes as the parameter.
|Parameters:||filename – Name of the file to download.|
Closes the FTP connection.