基於paramiko將檔案上傳到伺服器上

2022-02-25 18:10:33 字數 1242 閱讀 4099

通過安裝使用paramiko模組,將本地檔案上傳到伺服器上

import

paramiko

import

datetime

import

oshostname = '

伺服器ip

'username = '

root

'password = '

伺服器密碼

'port = 22

#配置資訊可以寫到配置檔案中

#loacl_file是要上傳的本地檔案路徑

#remote_path是要上傳到伺服器上指定檔案的路徑

defupload(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)

paramiko上傳檔案到Linux

importparamiko transport paramiko.transport host 22 transport.connect username root password 123 sftp paramiko.sftpclient.from transport transport sft...

paramiko中putof檔案上傳問題優化

paramiko模組大檔案上傳 1.遇到paramiko大檔案上傳後端速度瓶頸 def put file by fl self,fl,remote path,file size none callback none with self as cli sftp cli.open sftp print ...

基於SpringMVC的檔案上傳

簡單步驟 1 pom引入約束 2 springmvc config.xml 配置檔案配置 3 前端 傳送檔案到controller 4 controller接收檔案 1 pom引入約束 1.2.1 commons fileupload commons fileupload 2 springmvc c...