• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Modules In Python / Python Secure FTP module

Python Secure FTP module

Author: PFB Staff Writer
Last Updated: August 27, 2020

Overview

In the previous post we covered the ftplib module in Python, which you can read
more about here. In this post we will cover the pysftp module.

SFTP (Secure File Transfer Protocol) is used for securely exchanging files
over the Internet.

What is it?

pysftp is an easy to use sftp module that utilizes paramiko and pycrypto.

It provides a simple interface to sftp.

Some of the features are:
Gracefully handles both RSA and DSS private key files automatically

Supports encrypted private key files.

Logging can now be enabled/disabled

Why should I use it?

When you want to securely exchange files over the Internet.

How do I install it?

pysftp is listed on PyPi and can be installed using pip.

# Search for pysftp
pip search pysftp

pysftp                    # - A friendly face on SFTP

#Install pysftp
pip install pysftp

How do I use it?

Using pysftp is easy and we will show some examples on how you can use it

List a remote directory

To connect to our FTP server, we first have to import the pysftp module and
specify (if applicable) server, username and password credentials.

After running this program, you should see all the files and directories of
the current directory of your FTP server.

import pysftp

srv = pysftp.Connection(host="your_FTP_server", username="your_username",
password="your_password")

# Get the directory and file listing
data = srv.listdir()

# Closes the connection
srv.close()

# Prints out the directories and files, line by line
for i in data:
    print i

Connection parameters

Arguments that are not given are guessed from the environment.

host

The Hostname of the remote machine.

username

Your username at the remote machine.(None)

private_key

Your private key file.(None)

password

Your password at the remote machine.(None)

port

The SSH port of the remote machine.(22)

private_key_pass

password to use if your private_key is encrypted(None)

log

log connection/handshake details (False)

Download / Upload a remote file

As in the previous example we first import the pysftp module and specify
(if applicable) server, username and password credentials.

We also import the sys module, since we want the user to specify the file to
download / upload.

import pysftp
import sys

# Defines the name of the file for download / upload
remote_file = sys.argv[1]

srv = pysftp.Connection(host="your_FTP_server", username="your_username",
password="your_password")

# Download the file from the remote server
srv.get(remote_file)

# To upload the file, simple replace get with put. 
srv.put(remote_file)

# Closes the connection
srv.close()

What’s the next step?

Play around with the script, change things and see what happens.

Try add error handling to it. What happens if no argument is passed?

Add some interaction to the program by prompting for input.

Sources

https://code.google.com/p/pysftp/
http://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Modules In Python, pysftp Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us