python基于paramiko将文件上传到服务器代码实现

时间:2022-04-24 12:31:27

python通过安装使用paramiko模块,将本地文件上传到服务器上

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import paramiko
import datetime
import os
 
hostname = '服务器ip'
username = 'root'
password = '服务器密码'
port = 22#配置信息可以写到配置文件中
#loacl_file是要上传的本地文件路径
#remote_path是要上传到服务器上指定文件的路径
def upload(local_file, remote_path):
  try:
    t = paramiko.Transport((hostname, port))
    t.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(t)
    print('开始上传文件%s ' % datetime.datetime.now())
 
    try:
      sftp.put(local_file, remote_path)
    except Exception as e:
      sftp.mkdir(os.path.split(remote_path)[0])
      sftp.put(local_file, remote_path)
      print("从本地: %s 上传到: %s" % (local_file, remote_path))
    print('文件上传成功 %s ' % datetime.datetime.now())
    t.close()
  except Exception as e:
    print(repr(e))
 
 
if __name__ == '__main__':
  local_file = r'/home/shl/dataETL/timings/words/word_pos.csv'
  remote_path = os.path.join('/home/',"word_pos.csv")
  upload(local_file, remote_path)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/sui776265233/p/10910219.html