开发者

Paramiko connection issue

开发者 https://www.devze.com 2023-02-05 20:16 出处:网络
I\'m writing my first desktop app and I\'m struggling with class instances. This app is a simple ftp program using paramiko. What I\'ve set up so far is a connection.py which looks like this...

I'm writing my first desktop app and I'm struggling with class instances. This app is a simple ftp program using paramiko. What I've set up so far is a connection.py which looks like this...

#connect.py

import user, db
import para开发者_如何学Pythonmiko, time, os

paramiko.util.log_to_file('paramiko-log.txt')
class Connection:
    def __init__(self):
        #Call DB Functions
        database = db.Database()
        #Set Transport
        self.transport = paramiko.Transport((user.hostname, user.port))

        #User Credentials
        username = user.username
        password = user.password
        self.transport.connect(username = username, password = password)

        self.sftp = paramiko.SFTPClient.from_transport(self.transport)

        print "Set your credentials in user.py for now!"
        msg = "Connecting as: %s, on port number %d" % (user.username, user.port)
        print msg

    def disconnect(self):
        print "Closing connection..."
        self.sftp.close()
        self.transport.close()
        print "Connection closed."

Pretty straightforward. Connect and disconnect. This connect.py file is being imported into a main.py (which is my gui)

#main.py

import connect
from PySide import QtCore, QtGui

class Window(QtGui.QWidget):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        windowWidth = 550
        windowHeight = 350

        self.establishedConnection = ""

        connectButton = self.createButton("&Connect", self.conn)
        disconnectButton = self.createButton("&Disconnect", self.disconnect)
        grid = QtGui.QGridLayout()
        grid.addWidget(connectButton, 3, 3)
        grid.addWidget(disconnectButton, 4, 3)
        grid.addWidget(self.createList(), 1, 0, 1, 4)

        self.setLayout(grid)     

        self.resize(windowWidth, windowHeight)
        self.setWindowTitle("FTP Program")

    def conn(self):
        connection = connect.Connection()
        self.establishedConnection = connection

    def disconnect(self):
        self.establishedConnection.disconnect()

    def createButton(self, text, member):
        button = QtGui.QPushButton(text)
        button.clicked.connect(member)
        return button

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    gui = Window()
    gui.show()
    sys.exit(app.exec_())

The issue is disconnecting. I was thinking __init__ would create an instance of the Connection() class. If you look on main.py you can see that I tried to create the variable self.connectionEstablished in order to save the object so I could call disconnect on it later.

Where am I going wrong? I'm fairly new to python and other non-web languages(I spend most of my time writing RoR and php apps).

No errors are shown at any time and I started this app out as a terminal app so I do know that connect.py does work as intended.

Edit: So I guess Senderle got a connection closed message, which is what I'd like to see as well but I'm not. I'll mark a best answer if I see something that solves my problem.

Edit Solved: Pushed connect.py and main.py into one file to simplify things. And for some reason that solved things. So who knows whats going on. I'm still going to hold off on 'best answer'. If someone can tell me why I can't have a split file like that then I'm all ears.


I tried the code and it ran fine. I made only a few changes.

First, I didn't know what "user" and "db" are, so I commented out

import user, db

and

database = db.Database()

and used my own data for username, password, etc.

Second, the PySide module isn't available via my package manager, so I used PyQt4 instead. It didn't like grid.addWidget(self.createList(), 1, 0, 1, 4) so I commented that out, and everything worked as expected.

Further thoughts: When there were connection errors, there was some console feedback consisting of stack traces, but nothing more, and self.establishedConnection remained a string, causing self.establishedConnection.disconnect() to fail. So perhaps there's a connection problem?

EDIT: Aaaahhhhh, I just saw this: "No errors are shown at any time." Are you running this from a terminal or double-clicking an executable? If you start it from a terminal, I bet you'll see stacktraces in the terminal. The gui doesn't close when the code hits an exception.

EDIT2: If joining the files fixes the problem, then I am certain the problem cannot have anything to do with python itself. This has to be a problem with eclipse. You say that connection.py began as a terminal app, so you must be able to run python apps from the command line. Try the following: put main.py, connect.py, etc. in a directory of their own, open a terminal, and run python main.py. If it works as expected, then the problem has something to do with eclipse.


You are not calling conn() in the constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消