如何在Django中列出DigitalOcean服务器文件?及Paramiko连接问题排查
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
600permissions (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 runserverlocally 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

