> For the complete documentation index, see [llms.txt](https://docs.couchdrop.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.couchdrop.io/walkthroughs/using-sftp-clients/using-python-with-sftp.md).

# Using Python with SFTP

Learn how to use Python with SFTP

Uploading and downloading files with Python is a great way to automate transfers or build out support for SFTP inside you application.

## How to use SFTP with Python

Using Python with SFTP (SSH File Transfer Protocol) can be accomplished using the `paramiko` library, which allows you to create SSH connections and perform file operations securely. Here's a basic guide on how to use Python with SFTP using `paramiko`:

1. **Install Paramiko**: Before you can use Paramiko, you need to install it. You can install it via pip:

   ```bash
   pip install paramiko
   ```
2. **Connect to the SFTP Server**: Below is an example of how to connect to an SFTP server using Paramiko:

   ```python
   import paramiko

   # Define SFTP connection parameters
   hostname = 'sftp.example.com'
   port = 22
   username = 'your_username'
   password = 'your_password'

   # Create an SSH client
   ssh_client = paramiko.SSHClient()
   ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

   # Connect to the SFTP server
   ssh_client.connect(hostname, port, username, password)

   # Create an SFTP session
   sftp = ssh_client.open_sftp()

   # Now you can perform SFTP operations
   ```
3. **Perform SFTP Operations**: Once you've established an SFTP connection, you can perform various operations such as uploading files, downloading files, listing directory contents, creating directories, etc. Here are a few examples:
   * **Upload a file**:

     ```python
     local_file = '/path/to/local/file.txt'
     remote_file = '/path/to/remote/file.txt'
     sftp.put(local_file, remote_file)
     ```
   * **Download a file**:

     ```python
     remote_file = '/path/to/remote/file.txt'
     local_file = '/path/to/local/file.txt'
     sftp.get(remote_file, local_file)
     ```
   * **List directory contents**:

     ```python
     directory = '/path/to/directory'
     files = sftp.listdir(directory)
     ```
   * **Create a directory**:

     ```python
     new_directory = '/path/to/new/directory'
     sftp.mkdir(new_directory)
     ```
4. **Close the Connection**: After you've finished with the SFTP operations, make sure to close the connection:

   ```python
   sftp.close()
   ssh_client.close()
   ```

By following these steps, you can use Python to interact with SFTP servers securely and perform various file operations. Make sure to handle exceptions and errors appropriately, especially when dealing with network connections and file operations.

## Using Paramiko and Python with Couchdrop

Couchdrop has full support for SFTP uploads with python using Paramiko. Simply use your Couchdrop hostname and credentials and you can connect to Couchdrop like any other SFTP server.&#x20;

Couchdrop also has a comprehensive API, that you can find [couchdrop-api](https://developers.couchdrop.io/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.couchdrop.io/walkthroughs/using-sftp-clients/using-python-with-sftp.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
