基於paramiko將文件上傳到服務器上

經過安裝使用paramiko模塊,將本地文件上傳到服務器上服務器

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)
相關文章
相關標籤/搜索