首页 > 代码库 > 一个最简单的ftpsever

一个最简单的ftpsever

没有什么事情可以做,无聊的很 写个最简单的ftp吧---说白了就是一个简单的文件上传。QAQ

思路:client --读取文件的一行 然后发到server端 然后server 读取 写入文件的一行 

先写客户端

#_*_coding:utf-8_*_import socket,sys,osdef upload_file(filename):    cli  = socket.socket()    ip_port = (‘localhost‘,8080)    cli.connect(ip_port)    cli.send(filename)    with open(filename,‘rb‘) as fe:        for i in fe.readlines():            cli  = socket.socket()            ip_port = (‘localhost‘,8080)            cli.connect(ip_port)            cli.send(i)def client(argv):    if len(argv)!=2:        sys.exit("2 argument expected but %s given" %len(argv))    msg = check_argv=(argv[1])    if msg:        upload_file(argv[1])def check_argv(filename):    if(os.path.isfile(filename)):        msg = "no file or directory --->%s"% filename        return msg    else:        return Noneif __name__ == "__main__":    print sys.argv[1]    client(sys.argv)

 然后是 server端

#_*_coding:utf-8_*_import socketdef main():    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)    s.bind((‘localhost‘,8080))    s.listen(5)    conn,address = s.accept()    filename = conn.recv(1024)    row_data = http://www.mamicode.com/filename[2:]"D:%s" % row_data    conn.close()    with open(filename,"w+") as fe:        while 1:            conn,address = s.accept()            clidata = http://www.mamicode.com/conn.recv(1024)"------->"            fe.write(clidata)            fe.flush()if __name__ == "__main__":    main()

  因为是在本机器上面做的 请忽略ip吧

一个最简单的ftpsever