码迷,mamicode.com
首页 > 其他好文 > 详细

一个最简单的ftpsever

时间:2016-09-01 07:04:32      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

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

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

先写客户端

#_*_coding:utf-8_*_
import socket,sys,os


def 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 None

if __name__ == "__main__":
    print sys.argv[1]
    client(sys.argv)

 然后是 server端

#_*_coding:utf-8_*_
import socket
def 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 = filename[2:]
    filename = "D:%s" % row_data
    conn.close()
    with open(filename,"w+") as fe:
        while 1:
            conn,address = s.accept()
            clidata = conn.recv(1024)
            print clidata,"------->"
            fe.write(clidata)
            fe.flush()


if __name__ == "__main__":
    main()

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

一个最简单的ftpsever

标签:

原文地址:http://www.cnblogs.com/nerdlerss/p/5828218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!