我使用paramiko创建一个scp连接来上传服务器上的文件,但它不起作用。它不会引发错误,但文件不会到达服务器。 代码示例:
if __name__ == "__main__":
print "connecting to: ", host
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host,PORT))
t = paramiko.Transport(sock)
t.start_client()
t.auth_password(USERNAME,PASSWORD)
for filename in FILES:
scp_channel = t.open_session()
print "\tsending...",filename
remote_filename = "%s%s" % (REMOTE_FOLDER, filename)
f = file(filename,'rb')
scp_channel.exec_command('scp -v -t %s\n' % '/'.join(remote_filename.split('/')[:-1]))
scp_channel.send('C%s %d %s\n' %(oct(os.stat(filename).st_mode)[-4:],os.stat(filename)[6],remote_filename.split('/')[-1]))
scp_channel.sendall(f.read())
f.close()
scp_channel.close()
t.close()
sock.close()
except:
print "ERROR - couldn't connect to %s " % host
print ok!
输出:
connection to myServer.com
sending... toto.txt ok!
但它在命令行中有效:scp文件username@myServer.com:〜/
为什么paramiko不会引发错误? 你知道其他python scp解决方案吗?
提前感谢您的帮助!