{"id":5366,"date":"2013-06-17T13:57:14","date_gmt":"2013-06-17T13:57:14","guid":{"rendered":"https:\/\/www.pythonforbeginners.com\/?p=5366"},"modified":"2020-08-27T13:21:19","modified_gmt":"2020-08-27T18:21:19","slug":"python-secure-ftp-module","status":"publish","type":"post","link":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module","title":{"rendered":"Python Secure FTP module"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>In the previous post we covered the ftplib module in Python, which you can read<br \/>\nmore about <a title=\"ftplib\" href=\"https:\/\/www.pythonforbeginners.com\/code-snippets-source-code\/how-to-use-ftp-in-python\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. In this post we will cover the pysftp module.<\/p>\n<p>SFTP (Secure File Transfer Protocol) is used for securely exchanging files<br \/>\nover the Internet.<\/p>\n<h2>What is it?<\/h2>\n<p>pysftp is an easy to use sftp module that utilizes paramiko and pycrypto.<\/p>\n<p>It provides a simple interface to sftp.<\/p>\n<p><strong>Some of the features are:<\/strong><br \/>\nGracefully handles both RSA and DSS private key files automatically<\/p>\n<p>Supports encrypted private key files.<\/p>\n<p>Logging can now be enabled\/disabled<\/p>\n<h2>Why should I use it?<\/h2>\n<p>When you want to securely exchange files over the Internet.<\/p>\n<h2>How do I install it?<\/h2>\n<p>pysftp is listed on PyPi and can be installed using pip.<\/p>\n<pre><code class=\"language-python\"># Search for pysftp\npip search pysftp\n\npysftp                    # - A friendly face on SFTP\n\n#Install pysftp\npip install pysftp\n<\/code><\/pre>\n<h2>How do I use it?<\/h2>\n<p>Using pysftp is easy and we will show some examples on how you can use it<\/p>\n<h3>List a remote directory<\/h3>\n<p>To connect to our FTP server, we first have to import the pysftp module and<br \/>\nspecify (if applicable) server, username and password credentials.<\/p>\n<p>After running this program, you should see all the files and directories of<br \/>\nthe current directory of your FTP server.<\/p>\n<pre><code class=\"language-python\">import pysftp\n\nsrv = pysftp.Connection(host=\"your_FTP_server\", username=\"your_username\",\npassword=\"your_password\")\n\n# Get the directory and file listing\ndata = srv.listdir()\n\n# Closes the connection\nsrv.close()\n\n# Prints out the directories and files, line by line\nfor i in data:\n    print i\n<\/code><\/pre>\n<h2>Connection parameters<\/h2>\n<p>Arguments that are not given are guessed from the environment.<\/p>\n<h3><strong>host<\/strong><\/h3>\n<p>The Hostname of the remote machine.<\/p>\n<h3><strong>username<\/strong><\/h3>\n<p>Your username at the remote machine.(None)<\/p>\n<h3><strong>private_key<\/strong><\/h3>\n<p>Your private key file.(None)<\/p>\n<h3><strong>password<\/strong><\/h3>\n<p>Your password at the remote machine.(None)<\/p>\n<h3><strong>port<\/strong><\/h3>\n<p>The SSH port of the remote machine.(22)<\/p>\n<h3><strong>private_key_pass<\/strong><\/h3>\n<p>password to use if your private_key is encrypted(None)<\/p>\n<h3><strong>log<\/strong><\/h3>\n<p>log connection\/handshake details (False)<\/p>\n<h3>Download \/ Upload a remote file<\/h3>\n<p>As in the previous example we first import the pysftp module and specify<br \/>\n(if applicable) server, username and password credentials.<\/p>\n<p>We also import the sys module, since we want the user to specify the file to<br \/>\ndownload \/ upload.<\/p>\n<pre><code class=\"language-python\">import pysftp\nimport sys\n\n# Defines the name of the file for download \/ upload\nremote_file = sys.argv[1]\n\nsrv = pysftp.Connection(host=\"your_FTP_server\", username=\"your_username\",\npassword=\"your_password\")\n\n# Download the file from the remote server\nsrv.get(remote_file)\n\n# To upload the file, simple replace get with put. \nsrv.put(remote_file)\n\n# Closes the connection\nsrv.close()\n<\/code><\/pre>\n<h2>What&#8217;s the next step?<\/h2>\n<p>Play around with the script, change things and see what happens.<\/p>\n<p>Try add error handling to it. What happens if no argument is passed?<\/p>\n<p>Add some interaction to the program by prompting for input.<\/p>\n<h5>Sources<\/h5>\n<p><a title=\"pysftp\" href=\"https:\/\/code.google.com\/p\/pysftp\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/code.google.com\/p\/pysftp\/<\/a><br \/>\n<a title=\"sftp\" href=\"http:\/\/en.wikipedia.org\/wiki\/SSH_File_Transfer_Protocol\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/en.wikipedia.org\/wiki\/SSH_File_Transfer_Protocol<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[128,211],"tags":[],"class_list":{"0":"post-5366","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-modules-in-python","7":"category-pysftp","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Secure FTP module - PythonForBeginners.com<\/title>\n<meta name=\"description\" content=\"Python Secure FTP module will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Secure FTP module - PythonForBeginners.com\" \/>\n<meta property=\"og:description\" content=\"Python Secure FTP module will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module\" \/>\n<meta property=\"og:site_name\" content=\"PythonForBeginners.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/pythonbeginners\" \/>\n<meta property=\"article:published_time\" content=\"2013-06-17T13:57:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-27T18:21:19+00:00\" \/>\n<meta name=\"author\" content=\"PFB Staff Writer\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonbeginners\" \/>\n<meta name=\"twitter:site\" content=\"@pythonbeginners\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PFB Staff Writer\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module\"},\"author\":{\"name\":\"PFB Staff Writer\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/person\\\/1f49841b9ba76108d92e7ef1ca5ee648\"},\"headline\":\"Python Secure FTP module\",\"datePublished\":\"2013-06-17T13:57:14+00:00\",\"dateModified\":\"2020-08-27T18:21:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module\"},\"wordCount\":361,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\"},\"articleSection\":[\"Modules In Python\",\"pysftp\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module\",\"name\":\"Python Secure FTP module - PythonForBeginners.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#website\"},\"datePublished\":\"2013-06-17T13:57:14+00:00\",\"dateModified\":\"2020-08-27T18:21:19+00:00\",\"description\":\"Python Secure FTP module will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/modules-in-python\\\/python-secure-ftp-module#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Secure FTP module\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\",\"name\":\"PythonForBeginners.com\",\"description\":\"Learn By Example\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#organization\",\"name\":\"PythonForBeginners.com\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/PFB-Logo-Final.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/PFB-Logo-Final.png\",\"width\":1868,\"height\":318,\"caption\":\"PythonForBeginners.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/pythonbeginners\",\"https:\\\/\\\/x.com\\\/pythonbeginners\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/#\\\/schema\\\/person\\\/1f49841b9ba76108d92e7ef1ca5ee648\",\"name\":\"PFB Staff Writer\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g\",\"caption\":\"PFB Staff Writer\"},\"url\":\"https:\\\/\\\/www.pythonforbeginners.com\\\/author\\\/pfb_staff\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Secure FTP module - PythonForBeginners.com","description":"Python Secure FTP module will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module","og_locale":"en_US","og_type":"article","og_title":"Python Secure FTP module - PythonForBeginners.com","og_description":"Python Secure FTP module will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","og_url":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module","og_site_name":"PythonForBeginners.com","article_publisher":"https:\/\/www.facebook.com\/pythonbeginners","article_published_time":"2013-06-17T13:57:14+00:00","article_modified_time":"2020-08-27T18:21:19+00:00","author":"PFB Staff Writer","twitter_card":"summary_large_image","twitter_creator":"@pythonbeginners","twitter_site":"@pythonbeginners","twitter_misc":{"Written by":"PFB Staff Writer","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module#article","isPartOf":{"@id":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module"},"author":{"name":"PFB Staff Writer","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/person\/1f49841b9ba76108d92e7ef1ca5ee648"},"headline":"Python Secure FTP module","datePublished":"2013-06-17T13:57:14+00:00","dateModified":"2020-08-27T18:21:19+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module"},"wordCount":361,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonforbeginners.com\/#organization"},"articleSection":["Modules In Python","pysftp"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module","url":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module","name":"Python Secure FTP module - PythonForBeginners.com","isPartOf":{"@id":"https:\/\/www.pythonforbeginners.com\/#website"},"datePublished":"2013-06-17T13:57:14+00:00","dateModified":"2020-08-27T18:21:19+00:00","description":"Python Secure FTP module will help you improve your python skills with easy to follow examples and tutorials. Click here to view code examples.","breadcrumb":{"@id":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonforbeginners.com\/modules-in-python\/python-secure-ftp-module#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonforbeginners.com\/"},{"@type":"ListItem","position":2,"name":"Python Secure FTP module"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonforbeginners.com\/#website","url":"https:\/\/www.pythonforbeginners.com\/","name":"PythonForBeginners.com","description":"Learn By Example","publisher":{"@id":"https:\/\/www.pythonforbeginners.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonforbeginners.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonforbeginners.com\/#organization","name":"PythonForBeginners.com","url":"https:\/\/www.pythonforbeginners.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonforbeginners.com\/wp-content\/uploads\/2020\/05\/PFB-Logo-Final.png","contentUrl":"https:\/\/www.pythonforbeginners.com\/wp-content\/uploads\/2020\/05\/PFB-Logo-Final.png","width":1868,"height":318,"caption":"PythonForBeginners.com"},"image":{"@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/pythonbeginners","https:\/\/x.com\/pythonbeginners"]},{"@type":"Person","@id":"https:\/\/www.pythonforbeginners.com\/#\/schema\/person\/1f49841b9ba76108d92e7ef1ca5ee648","name":"PFB Staff Writer","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/43a6730888902641f2b8e13d1eb78522f178077d85b17a796302635c434a3ac0?s=96&d=mm&r=g","caption":"PFB Staff Writer"},"url":"https:\/\/www.pythonforbeginners.com\/author\/pfb_staff"}]}},"jetpack_publicize_connections":[],"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"PFB Staff Writer","author_link":"https:\/\/www.pythonforbeginners.com\/author\/pfb_staff"},"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/5366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/comments?post=5366"}],"version-history":[{"count":3,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/5366\/revisions"}],"predecessor-version":[{"id":7416,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/posts\/5366\/revisions\/7416"}],"wp:attachment":[{"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/media?parent=5366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/categories?post=5366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonforbeginners.com\/wp-json\/wp\/v2\/tags?post=5366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}