You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django中列出DigitalOcean服务器文件?及Paramiko连接问题排查

Fixing Issues in Your Django Cloud Panel Project

Hey there, let's work through each of your problems step by step to get your panel running smoothly:

1. Resolve Paramiko Connection Issues First

Since directory validation depends on a working Paramiko connection, let's nail this down first. Even if you know Paramiko is functional, your integration might have subtle bugs.

  • Start with a minimal test script to isolate the connection logic:
    import paramiko
    
    try:
        # Initialize SSH client
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        
        # Use password auth (swap with key-based if you use that)
        ssh_client.connect(
            hostname="your-server-ip",
            username="your-ssh-user",
            password="your-ssh-pass",
            timeout=10
        )
        
        # Test with a simple command
        stdin, stdout, stderr = ssh_client.exec_command("echo 'Connection works!'")
        print(stdout.read().decode().strip())
        ssh_client.close()
    except Exception as e:
        print(f"Connection failed: {str(e)}")
    
  • Check these common pitfalls:
    • Is port 22 open on your server's firewall/security group?
    • If using SSH keys: Ensure your local private key has 600 permissions (chmod 600 ~/.ssh/your-key) and the public key is added to the server's ~/.ssh/authorized_keys.
    • Did you disable password authentication on the server? If so, you must use key-based auth.

2. Fix the ls -p | grep -v / Command Issue

That shell command might behave unpredictably across different server environments, or fail if the remote shell doesn't handle pipes correctly. A more reliable approach is to use Paramiko's SFTP to list files directly:

# After establishing a successful SSH connection
sftp = ssh_client.open_sftp()
non_directory_files = []

for file_attr in sftp.listdir_attr("."):
    # Check if the entry is NOT a directory (using file mode bits)
    if not (file_attr.st_mode & 0o40000):
        non_directory_files.append(file_attr.filename)

sftp.close()
print(non_directory_files)

If you really prefer using a shell command, switch to a more robust version like:

find . -maxdepth 1 -type f -printf "%f\n"

This avoids pipe-related issues and works consistently across most Unix-like systems.

3. Correct the manage.py Shebang Format

Your manage.py has a syntax error in the first line—you combined the shebang and import into one line, which breaks the script. Split it into separate lines:

#!/usr/bin/env python
import os
import sys

# Rest of your manage.py code...

The shebang line (#!/usr/bin/env python) tells the system which interpreter to use, and it must be on its own line at the very top.

4. Troubleshoot the "Manage Button" Exception Page

Once the above issues are fixed, if you still see the error page:

  • Check Django's debug logs (run python manage.py runserver locally and look at the console output) to get the exact exception traceback. This will tell you if the problem is a failed Paramiko connection, a bad command execution, or an unhandled error in your view.
  • Add try-except blocks in your view function to catch and handle exceptions gracefully, instead of letting Django throw a 500 error. For example:
    def manage_server(request):
        try:
            # Your Paramiko connection and directory validation code here
            return render(request, "manage.html", {"files": non_directory_files})
        except Exception as e:
            return render(request, "error.html", {"message": f"Failed to connect: {str(e)}"})
    

内容的提问来源于stack exchange,提问作者Safa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:27:18