python操作服务器

一:使用 paramiko 进行SSH连接

首先确保已安装paramiko库:pip install paramiko

代码示例

python 复制代码
在这里插入代码片import paramiko

hostname = 'hostname'
username = 'user'
password = 'passwd'

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, password=password)

stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
    print(line.strip('\n'))
    
client.close()

二:使用 smtplib 发送邮件到SMTP服务器

代码示例

python 复制代码
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 邮件发送者和接收者
sender = 'from@example.com'
receivers = ['to@example.com']

# 邮件内容
message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')
message['From'] = Header("Python教程", 'utf-8')
message['To'] =  Header("测试", 'utf-8')
message['Subject'] = Header('Python SMTP 邮件测试', 'utf-8')

try:
    smtpObj = smtplib.SMTP('localhost')
    smtpObj.sendmail(sender, receivers, message.as_string())    
    print("邮件发送成功")
except smtplib.SMTPException as e:
    print("Error: 无法发送邮件", e)

三:上传文件到服务器

python 复制代码
import paramiko

def upload_file_to_server(hostname, port, username, password, local_file_path, remote_file_path):
    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname, port=port, username=username, password=password)

        ftp_client = client.open_sftp()
        ftp_client.put(local_file_path, remote_file_path)
        ftp_client.close()

        client.close()
        print("文件上传成功")
    except Exception as e:
        print(f"文件上传失败: {str(e)}")

# 示例
upload_file_to_server('hostname', 22, 'username', 'password', '/path/to/local/file', '/path/to/remote/file')

四:从服务器下载文件

代码示例

python 复制代码
import paramiko

def download_file_from_server(hostname, port, username, password, remote_file_path, local_file_path):
    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname, port=port, username=username, password=password)

        ftp_client = client.open_sftp()
        ftp_client.get(remote_file_path, local_file_path)
        ftp_client.close()

        client.close()
        print("文件下载成功")
    except Exception as e:
        print(f"文件下载失败: {str(e)}")

# 示例
download_file_from_server('hostname', 22, 'username', 'password', '/path/to/remote/file', '/path/to/local/file')

五:在服务器上执行命令

python 复制代码
import paramiko

def execute_command_on_server(hostname, port, username, password, command):
    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname, port=port, username=username, password=password)

        stdin, stdout, stderr = client.exec_command(command)
        print("命令输出:")
        for line in stdout:
            print(line.strip())

        client.close()
    except Exception as e:
        print(f"执行命令失败: {str(e)}")

# 示例
execute_command_on_server('hostname', 22, 'username', 'password', 'ls')

往服务器传文件时报错:

python 复制代码
CryptographyDeprecationWarning: Blowfish has been deprecated and will be removed in a future release
  "class": algorithms.Blowfish,

解决方案:

报错文件里面 以下的代码注释掉即可

相关推荐
紫雾凌寒41 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
程序员杰哥1 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
tekin8 小时前
基于 Python 开发在线多人游戏服务器案例解析
服务器·python·游戏·在线多人游戏服务器
让学习成为一种生活方式10 小时前
libGL.so.1: cannot open shared object file: No such file or directory-linux022
linux·开发语言·python
java1234_小锋10 小时前
一周学会Flask3 Python Web开发-Jinja2模板继承和include标签使用
python·flask·flask3
图书馆钉子户11 小时前
from flask_session import Session 为什么是Session(app)这么用?
python·flask·mybatis
一只蜗牛儿11 小时前
Sherpa-ONNX:说话人识别与语音识别自动开启(VAD)+ Python API 完整指南
人工智能·python·语音识别
结衣结衣.11 小时前
【OpenCV】入门教学
图像处理·人工智能·python·opencv