python文件传输带界面的客户端

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from Tkinter import *
import tkFileDialog as dialog

from socket import *
import os
import yaml


def client(filename):


    file=open('config.yaml','r')
    conf=yaml.load(file)
    HOST=conf['clienthost']
    PORT=conf['port']
    file.close()
    
    ADDR = (HOST,PORT)
    
    BUFSIZE = 1024
    
    FILEINFO_SIZE=os.path.getsize(filename)
    print FILEINFO_SIZE
    tfilename=filename[filename.rfind('/')+1:len(filename)]
    fhead='%d \n %s'% (FILEINFO_SIZE,tfilename)
    
    sendSock = socket(AF_INET,SOCK_STREAM)
    sendSock.connect(ADDR)
    
    sendSock.send(fhead)
    
    fp = open(filename,'rb')
    
    while 1:
    
        filedata = fp.read(BUFSIZE)
    
        if not filedata: break
    
        sendSock.send(filedata)
    
    print "文件传送完毕,正在断开连接..."
    
    fp.close()
    
    sendSock.close()
    
    print "连接已关闭..."


def select(root,labelx):

    filename = dialog.askopenfilenames(
      parent=root,
      filetypes=[('Text' , '*.*' )],
      title='Save as...' )
    labelx.config(text=filename)
    path=labelx.cget('text')
    outpath=path[1:len(path)-1]
#    print outpath

def transfer(labelx):
    
    path=labelx.cget('text')

    outpath=path[1:len(path)-1]
    
#    print type(outpath)
#    print outpath
    client(outpath)


window = Tk()
frame = Frame(window)
frame.pack()
labelp = Label(frame, text='file name                      ')
labelp.pack()


buttonx=Button(frame,text="open",command=lambda:select(window,labelp))
buttonx.pack(side="left")

buttony=Button(frame,text="Transfer",command=lambda:transfer(labelp))
buttony.pack(side="left")

window.mainloop()

用到的yamal文件和服务器端一样

你可能感兴趣的:(python文件传输带界面的客户端)