在客户端上使用tkFileDialog运行服务器的文件打开对话框

时间:2016-08-12 12:14:44

标签: python python-2.7 sockets tkinter operating-system

我正在尝试创建一个简单的系统来将文件从服务器传输到客户端。 我想使用tkFileDialog向客户端显示一个文件打开对话框。 问题是当我运行客户端&服务器对话框在服务器而不是客户端打开。我想过通过

发送tkFileDialog的对象

con.send(str(tkFileDialog.askopenfilename(initialdir='~', title='Choose a text file/program')))

如果有人可以帮我解决这个问题,那将会非常有帮助,或者如果有人有更好的想法在没有ssh的客户端中打开服务器文件系统

完整的代码是

server.py:

import socket,os
import Tkinter,tkFileDialog,tkMessageBox



def startServer(portName,ip):
    if portName != '' and ip != '':
        tkMessageBox.showinfo('Server Started','Server running!!! ok will be enabled after transfer')
        port = int(portName)
        ipName = ip
        sd = socket.socket()
        sd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)   #reuse socket
        sd.bind((ipName, port))
        sd.listen(50)

        con, addr = sd.accept()
        print ' connection from ' + str(addr)
        con.send(tkMessageBox.showinfo('connected', 'Connection Successful'))

        while True:

            con.send(str(tkFileDialog.askopenfilename(initialdir='~', title='Choose a text file/program')))
            fileN = con.recv(1024)

            if os.path.isfile(fileN):

                con.send(tkMessageBox.showinfo('Process completed', 'Rerun server to transfer again'))
                con.send('exists')
                fileN = str(fileN)


                #read contents

                fd = open(fileN, 'r')
                buff = fd.read()
                print buff
                print len(buff)
                fd.close()

                #send contents
                con.send(str(len(buff)-1))
                print con.recv(14)    #acknowledgement of length received
                con.send(buff)
                break
            else:
                con.send(tkMessageBox.showerror('Failed', 'Select appropriate file'))
                con.send('ne')
        sd.close()
    else:
        tkMessageBox.showerror('Failed','Failed to start server. Give appropriate inputs')

def main():

    root = Tkinter.Tk()
    root.geometry('300x200')
    root.title('Server')

    ipLabel = Tkinter.Label(root,text='\nEnter IP Address of server\n')
portLabel = Tkinter.Label(root, text='\nEnter Port Address of server\n')

    ipEntry = Tkinter.Entry(root)
    portEntry = Tkinter.Entry(root)

    connectButton = Tkinter.Button(root,
                               text='Run',
                               command=lambda: startServer(portEntry.get(), ipEntry.get())
                               )

   ipLabel.pack()
   ipEntry.pack()
   portLabel.pack()
   portEntry.pack()
   connectButton.pack()

   root.mainloop()


main()

client.py:

import socket
import Tkinter,tkMessageBox

def establishConnection(ipEntry,portEntry,root):

    if ipEntry != '' and portEntry != '':

        port = int(portEntry)
        ipName = str(ipEntry)

        sd = socket.socket()
        sd.connect((ipName, port))
        # ack if connection established
        sd.recv(1024)

        while True:

            #file select dialog
            fileN = sd.recv(1024)
            sd.send(fileN)          #send file name to open

            sd.recv(1024)           #dialog after file selected or not
            fileAck = sd.recv(6)    #acknowledge if file correct

            if fileAck == 'exists':

                leng = int(sd.recv(10))
                sd.send('length recieved')
                buff = str(sd.recv(leng))

                saveLabel = Tkinter.Label(root, text='\n\nSave with Name\n')
                saveNameEntry = Tkinter.Entry(root)
                saveButton = Tkinter.Button(root,
                                        text='SAVE',
                                        command=lambda: saveFile(saveNameEntry.get(), buff, root))
                saveLabel.pack()
                saveNameEntry.pack()
                saveButton.pack()
                break

        sd.close()

def saveFile(fileN, buff, root):

    fd = open(fileN, 'w')
    fd.write(buff)
    fd.close()
    tkMessageBox.showinfo('Operation Complete','file saved as '+fileN)
    root.destroy()



def main():

    root = Tkinter.Tk()
    root.geometry('300x600')
    root.title('Client')


    ipLabel = Tkinter.Label(root,text='\nEnter IP Address of server\n')
    portLabel = Tkinter.Label(root, text='\nEnter Port Address of server\n')

    ipEntry = Tkinter.Entry(root)
    portEntry = Tkinter.Entry(root)

    connectButton = Tkinter.Button(root,
                               text='Connect',
                               command=lambda: establishConnection(ipEntry.get(), portEntry.get(),root)
                               )

    ipLabel.pack()
    ipEntry.pack()
    portLabel.pack()
    portEntry.pack()
    connectButton.pack()



    root.mainloop()

main()

1 个答案:

答案 0 :(得分:0)

文件对话框仅适用于打开它的系统。您无法使用它来选择远程系统上的文件。