paramiko的scp模块

scp的Python项目详细描述


py模块使用paramiko传输通过 SCP1协议。这是openssh scp程序中引用的协议, 只在这个实现中测试过。

示例

fromparamikoimportSSHClientfromscpimportSCPClientssh=SSHClient()ssh.load_system_host_keys()ssh.connect('example.com')# SCPCLient takes a paramiko transport as an argumentscp=SCPClient(ssh.get_transport())scp.put('test.txt','test2.txt')scp.get('test2.txt')# Uploading the 'test' directory with its content in the# '/home/user/dump' remote directoryscp.put('test',recursive=True,remote_path='/home/user/dump')scp.close()
$ md5sum test.txt test2.txt
fc264c65fb17b7db5237cf7ce1780769 test.txt
fc264c65fb17b7db5237cf7ce1780769 test2.txt

使用“with”关键字

fromparamikoimportSSHClientfromscpimportSCPClientssh=SSHClient()ssh.load_system_host_keys()ssh.connect('example.com')withSCPClient(ssh.get_transport())asscp:scp.put('test.txt','test2.txt')scp.get('test2.txt')
$ md5sum test.txt test2.txt
fc264c65fb17b7db5237cf7ce1780769 test.txt
fc264c65fb17b7db5237cf7ce1780769 test2.txt

上载类似文件的对象

putfo方法可用于上载类似文件的对象:

importiofromparamikoimportSSHClientfromscpimportSCPClientssh=SSHClient()ssh.load_system_host_keys()ssh.connect('example.com')# SCPCLient takes a paramiko transport as an argumentscp=SCPClient(ssh.get_transport())# generate in-memory file-like objectfl=io.BytesIO()fl.write(b'test')fl.seek(0)# upload it directly from memoryscp.putfo(fl,'/tmp/test.txt')# close connectionscp.close()# close file handlerfl.close()

跟踪文件上载/下载的进度

可以将progress函数作为对scpclient的回调来处理 当前SCP操作如何处理传输进度在 下面的示例我们打印文件传输的完成百分比。

fromparamikoimportSSHClientfromscpimportSCPClientimportsysssh=SSHClient()ssh.load_system_host_keys()ssh.connect('example.com')# Define progress callback that prints the current percentage completed for the filedefprogress(filename,size,sent):sys.stdout.write("%s\'s progress: %.2f%%\r"%(filename,float(sent)/float(size)*100))# SCPCLient takes a paramiko transport and progress callback as its arguments.scp=SCPClient(ssh.get_transport(),progress=progress)# you can also use progress4, which adds a 4th parameter to track IP and port# useful with multiple threads to track sourcedefprogress4(filename,size,sent,peername):sys.stdout.write("(%s:%s) %s\'s progress: %.2f%%\r"%(peername[0],peername[1],filename,float(sent)/float(size)*100))scp=SCPClient(ssh.get_transport(),progress4=progress4)scp.put('test.txt','~/test.txt')# Should now be printing the current progress of your put function.scp.close()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java播放流媒体音乐   LWJGL中的java纹理未显示   java从父集合中删除时删除子对象   mysql希望在我的代码中添加验证,如果表不存在,它应该用java创建新表   java我可以关闭客户端的socket而不引起服务器端的EOFEException吗?   java Primefaces对话框框架咆哮和showMessageInDialog不工作   hadoop配置单元无法初始化类java。网网络接口   关键字中缺少oracle11g Java iBatis   java在RESTAPI中创建PUT和POST端点,而不创建GET端点?   java Math abs和ceil输出编译错误   java Tomcat 8.017代md5及其摘要。球棒   java SpringBean配置xml文件在IteliJ Idea中加载   java为什么在使用Powershell指定Xms和Xmx时,它们什么都不做,但通过Netbeans IDE可以正常工作?   java Drools项目构建失败,kjar打包从7.7版开始