Paramiko的SFTP Python连接

时间:2017-01-13 17:52:48

标签: python ssh sftp paramiko

我正在尝试使用Paramiko的库http://docs.paramiko.org/en/2.1/api/sftp.html设置与Python(v2.7)的SFTP连接

通过在线的点点滴滴,我能够(我认为无论如何)使用私人加密证书进行连接。

这看起来如下:

import paramiko
sftp = paramiko.SSHClient()

hostname = "sftp.host.com"
port = 8022
username = "sftplogin"

k = paramiko.RSAKey.from_private_key_file("private.pem", password="XXX")
sftp.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "Connecting..."
sftp.connect(hostname, port, username=username, pkey=k )
print "Connected to: " + hostname + ":" + port
print sftp.getcwd()
sftp.close()

此时,我只是想看看我是不是妄想并且事实上没有连接,所以我试图打印一个getcwd()来获取当前目录......可悲的是我是无处可去,因为它正在返回这个错误:

  

AttributeError:'SSHClient'对象没有属性'getcwd'

谁能告诉我为什么?我做错了什么?

提前致谢!

2 个答案:

答案 0 :(得分:2)

这对我有用。

import os
import paramiko

host = "sftp.host.com"
port = 8022
transport = paramiko.Transport((host, port))
username = "sftplogin"
mykey = paramiko.RSAKey.from_private_key_file("private_export.pem", password="XXX")
print "Connecting..."
transport.connect(username = username, pkey = mykey)
sftp = paramiko.SFTPClient.from_transport(transport)
print "Connected."
print sftp.listdir()
sftp.close()
transport.close()
print "Closed connection."

答案 1 :(得分:1)

您实例化了一个paramiko.SSHClient对象,但是您尝试执行SFTP Client methods

您应该能够执行此操作来证明这一事实:

   <!--<div class="list card">
        <div class="item item-avatar">
                <img src="img/register.png">
                <h2>Customer Reservation/ Booking</h2>
                <p>Application for to make a reservation or advancement booking for customers.</p>
        </div>

        <div class="item item-image">
        <p>Reservation/Booking Form</p>

            <form class="reservation">
                <div class="list list-inset">

                    <label class="item item-input">
                        <i class="icon  ion-ios-personadd placeholder-icon"></i>  <input type="text" ng-model="reservation.name" placeholder="Name">
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-ios-infinite placeholder-icon"></i>  <input type="text" ng-model="reservation.ic" placeholder="IC/Passport Number">
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-ios-email placeholder-icon"></i>  <input type="email" ng-model="reservation.email" placeholder="Email">
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-iphone placeholder-icon"></i>  <input type="text" ng-model="reservation.phone" placeholder="Phone Number">
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-location placeholder-icon"></i>  
                        <input type="text" ng-model="reservation.address" placeholder="Address" />
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-android-home placeholder-icon"></i>
                        <select ng-model="reservation.warehouse" ng-controller="warehouseCtrl" ng-options="w.warehouse as w.warehouse for w in warehouse" />
                    </label>
                    <br>

                    <label class="item item-input">
                        <i class="icon ion-ios-calendar placeholder-icon"></i>  
                        <input type="date" ng-model="reservation.startDate" placeholder="Warehouse Booking Start Date" />
                    </label>



                </div>
            </form>

        </div>

        <a class="item item-icon-left assertive" href="#">
        <i class="icon ion-music-note"></i>
        Start listening
        </a>

    </div>-->







    <div class="list card">

    <div class="item item-avatar">
        <img src="img/register.png">
             <h2>Customer Reservation/ Booking</h2>
             <p>Application for to make a reservation or advancement booking for customers.</p>
    </div>

        <div class="item item-body">
            <p>Reservation/Booking Form</p>


            <form class="reservation" ng-controller="submitCtrl">
             <label class="item item-input">
                        <i class="icon  ion-ios-personadd placeholder-icon"></i>  <input type="text" ng-model="reservation.name" placeholder="Name">
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-ios-infinite placeholder-icon"></i>  <input type="text" ng-model="reservation.ic" placeholder="IC/Passport Number">
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-ios-email placeholder-icon"></i>  <input type="email" ng-model="reservation.email" placeholder="Email">
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-iphone placeholder-icon"></i>  <input type="text" ng-model="reservation.phone" placeholder="Phone Number">
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-location placeholder-icon"></i>  
                        <input type="text" ng-model="reservation.address" placeholder="Address" />
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-android-home placeholder-icon"></i>
                        <select ng-model="reservation.warehouse" ng-controller="warehouseCtrl" ng-options="w.warehouse as w.warehouse for w in warehouse"><option value="" disabled selected>Select your warehouse option</option></select>
                    </label>

                    <label class="item item-input">

                        <i class="icon ion-ios-calendar placeholder-icon"></i>  
                        <!--<input type="text" ng-model="reservation.startDate" placeholder="Warehouse Booking Start Date" onfocus="(this.type='date')"/>-->
                        <input type="date" ng-model="reservation.startDate" placeholder="Warehouse Booking Start Date" />
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-ios-calendar-outline placeholder-icon"></i>  
                        <!--<input type="text" ng-model="reservation.endDate" placeholder="Warehouse Booking End Date" onfocus="(this.type='date')"/>-->
                        <input type="date" ng-model="reservation.endDate" placeholder="Warehouse Booking End Date"/>
                    </label>

                    <label class="item item-input">
                        <i class="icon ion-checkmark-circled placeholder-icon"></i>  
                        <textarea ng-model="reservation.remark" placeholder="Any additional remarks..." rows="5"></textarea>
                    </label>

                     <!--<a ui-sref="tab-main" class="button button-positive button-block" ng-controller="submitCtrl" ng-click="submit()">Submit</a>-->
                     <a class="button button-positive button-block"  ng-click="submit()">Submit</a>
            </form>
        </div>

 </div>


 </ion-view>

但我认为你的意思是实例化SFTP类,而不是SSH客户端,所以这可能是你出错的地方。