首先,这是我在该网站上的第一个问题,所以我可能会犯一些基本的错误,提前道歉。无论如何,我试图为telnet制作一个GUI,我使用python3作为编程语言。但是我想要连续连接,遗憾的是,在我停止程序之前我无法获得任何输出(tn.write(b" exit \ n"))。我对各种反馈持开放态度(比如你应该使用套接字......)任何你提供的信息都将受到赞赏。 Ps:到目前为止我只尝试连接localhost,我只填充了连接按钮。 Ps2:不知怎的,我甚至无法完全记住代码。
from tkinter import *
import tkinter.messagebox
import getpass
import sys
import telnetlib
def main():
root= Tk()
def tel_net():
username = entry1.get()
pw = entry2.get()
host_ip= entry3.get()
HOST = host_ip
user = username
password = pw
tn = telnetlib.Telnet(HOST)
tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"ls\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii')
def printconnect():
print("connected")
tel_net()
tkinter.messagebox.showinfo("telnet", "connected")
def printdisconnect():
print("disconnected")
tn = telnetlib.Telnet(HOST)
tn.close(HOST)
tkinter.messagebox.showinfo("telnet","disconnected")
def quitwindow():
answer = tkinter.messagebox.askquestion("telnet","Do you want to quit ?")
if answer == "yes":
root.destroy()
button1 = Button(root, text="Connect", command=printconnect, fg="green")
button2 = Button(root, text="Disconnect", command=printdisconnect, fg="red")
button3 = Button(root, text="Exit", command=quitwindow, fg="purple")
button1.grid(row=3, column=0, sticky = E)
button2.grid(row=3, column=1, sticky = E)
button3.grid(row=3, column=2, sticky = E)
label1 = Label(root, text="Username")
label2 = Label(root, text= "Password")
label3 = Label(root, text="Host ip")
entry1 = Entry(root)
entry2 = Entry(root)
entry3 = Entry(root)
entry2.config(show="*");
label1.grid(row=0, column=0, sticky = W)
label2.grid(row=1, column=0, sticky = W)
label3.grid(row=2, column=0, sticky = W)
entry1.grid(row=0, column=1, sticky = W)
entry2.grid(row=1, column=1, sticky = W)
entry3.grid(row=2, column=1, sticky = W)
c1=Checkbutton(root, text="Remember username")
c1.grid(columnspan=2,sticky = W)
c2=Checkbutton(root, text="Remember password")
c2.grid(columnspan=2,sticky = W)
root.mainloop()
if __name__ == "__main__":
main()
答案 0 :(得分:0)
由于telnet通信阻塞,因此需要将实际通信放入GUI的单独线程中。您还需要对所有读取操作设置超时,然后检查是否在读取之间执行任何写入操作。