用python写一个本地前端文件更新到服务器的工具

源码

# -- coding: UTF-8 -

import paramiko
import os

# SSH服务器的IP地址、用户名和密码
hostname = '改成服务器的ip地址'
username = '改成用户名'
password = '改成密码'
#上传文件路径
remote_directory = '/data/public/front/'


def deploy_html(local_file_path, folder_name):
    # 创建SSH客户端对象
    client = paramiko.SSHClient()
    # 自动添加服务器的SSH密钥
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接到SSH服务器
    client.connect(hostname=hostname, username=username, password=password)

    # 服务器上文件地址
    remote_file_path = remote_directory + '/' + os.path.basename(local_file_path)

    # 先删除服务器上的旧文件
    command = 'rm -rf ' + remote_directory + '/' + folder_name

    # 创建SFTP客户端对象
    sftp = client.open_sftp()
    # 上传文件到远程服务器
    print('上传文件到远程服务器: ' + remote_file_path)
    sftp.put(local_file_path, remote_file_path)
    # 关闭SFTP客户端对象
    sftp.close()

    # 解压缩文件
    unzip_command = 'unzip ' + remote_file_path + ' -d ' + remote_directory
    command = command + '; ' + unzip_command
    print('exec_command: ' + command)

    stdin, stdout, stderr = client.exec_command(command, get_pty=True)
    # 打印命令输出
    print(stdout.read().decode('utf-8'))

    # 关闭SSH客户端对象
    client.close()
    print(local_file_path + "上传成功")
    pass


# 默认目录为py文件所在文件目录
current_dir = os.path.dirname(os.path.abspath(__file__))
files = os.listdir(current_dir)
for file_name in files:
    if file_name.endswith('.zip'):
        file_path = os.path.join(current_dir, file_name)
        print(file_path)
        print(file_name.split('.')[0])
        deploy_html(file_path, file_name.split('.')[0])

运行方式

将前端文件包放到和python文件同一个目录下,运行python deploy-html.py

window下也可以创建一个启动脚本start.bat,不用每次敲命令行

python ./deploy-html.py
pause
posted @   今晚煮鸡蛋  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示