Paramiko 连接问题
我正在编写我的第一个桌面应用程序,并且正在努力处理类实例。这个应用程序是一个使用 paramiko 的简单 ftp 程序。到目前为止我所设置的是一个connection.py,它看起来像这样......
#connect.py
import user, db
import paramiko, 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."
非常简单。连接和断开。 这个 connect.py 文件正在导入到 main.py (这是我的 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_())
问题是断开连接。 我认为 __init__ 会创建一个 Connection() 类的实例。如果您查看 main.py,您会发现我尝试创建变量 self.connectionEstablished 来保存该对象,以便稍后可以对其调用断开连接。
我哪里出错了?我对 python 和其他非网络语言相当陌生(我大部分时间都在编写 RoR 和 php 应用程序)。
任何时候都不会显示错误,并且我将此应用程序作为终端应用程序启动,因此我确实知道 connect.py 确实按预期工作。
编辑:所以我猜 Senderle 收到了一条连接已关闭的消息,这也是我想看到的,但我没有。如果我看到可以解决我的问题的内容,我会标记为最佳答案。
编辑已解决:将 connect.py 和 main.py 推送到一个文件中以简化操作。出于某种原因,解决了问题。所以谁知道发生了什么事。我仍然会推迟“最佳答案”。如果有人能告诉我为什么我不能有这样的分割文件,那么我会洗耳恭听。
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 paramiko, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试了代码并且运行良好。我只做了一些改变。
首先,我不知道“user”和“db”是什么,所以我注释掉
并
使用我自己的数据作为用户名、密码等。
其次,PySide 模块无法通过我的包管理器使用,所以我使用 PyQt4 代替。它不喜欢 grid.addWidget(self.createList(), 1, 0, 1, 4) ,所以我将其注释掉,一切都按预期进行。
进一步的想法:当出现连接错误时,有一些由堆栈跟踪组成的控制台反馈,但仅此而已,并且
self.builtedConnection
仍然是一个字符串,导致self.builtedConnection.disconnect()< /code> 失败。那么也许存在连接问题?
编辑:啊啊啊啊啊,我刚刚看到这个:“任何时候都不会显示错误。”您是从终端运行它还是双击可执行文件?如果您从终端启动它,我敢打赌您会在终端中看到堆栈跟踪。当代码遇到异常时,GUI 不会关闭。
EDIT2:如果加入文件可以解决问题,那么我确信问题与 python 本身没有任何关系。这一定是eclipse的问题。您说 connection.py 最初是一个终端应用程序,因此您必须能够从命令行运行 python 应用程序。尝试以下操作:将 main.py、connect.py 等放在自己的目录中,打开终端,然后运行 python main.py。如果它按预期工作,那么问题与 eclipse 有关。
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
and
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, causingself.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.您没有在构造函数中调用 conn() 。
You are not calling conn() in the constructor.