【问题标题】:FTP server Client using ftplib使用 ftplib 的 FTP 服务器客户端
【发布时间】:2013-05-19 22:29:20
【问题描述】:

我正在尝试同时实现 FTP 服务器和客户端来传输文件,但我基本上无法使用 python 中的ftplib 模块在两者之间建立连接......

这就是我所做的,我该如何修复代码? 还有其他方法吗?

我的服务器代码:-

       #making the important imports for file transfer :

       from ftplib import FTP

       ftp_object_server=FTP()
       #s = socket.socket()         
       host = "121.0.0.1"
       port = 1227
       ftp_object_server.connect(host,port)


       while True:
            c, addr = ftp_object_server.accept()
            print c
            print addr# Establish connection with client.
            print 'Got connection from', addr
            c.send('Thank you for connecting')

            ftp_object_server.close()                # Close the connection

我的客户的代码:-

     from ftplib import FTP


     ftp_object_client=FTP()

     host = "121.0.0.1" # Get local machine name
     port = 1227                # Reserve a port for your service.

     #---------Creating My Own Set of username and passwords ------------------------#
     Username=['abc','efg','hij']
     Password=['123']


     input_user_name=raw_input('Enter the Username')
     input_user_password=raw_input('Enter the password')           

     if input_user_name in Username and input_user_password  in Password:
         ftp_object_client.connect(host)
         print ftp_object_client.recv(1024)

         ftp_object_client.close()

【问题讨论】:

    标签: python ftp-client ftp-server


    【解决方案1】:

    标准库中的ftplib 模块仅涵盖FTP 协议的客户端部分。 所以你所做的是试图打开两个到121.0.0.1 的连接。我假设你的意思是localhost,反正就是127.0.0.1

    不知道你真正想要达到什么目标,有一些选择:

    • 使用HTTP传输文件,使用python -m SimpleHTTPServer(或python3中的python3 -m http.server)和requests作为客户端
    • 使用现有的解决方案(请参阅One line ftp server in python 的答案)
    • 实现您自己的服务器

    【讨论】:

    • 我会在 python 中使用单行 ftp 服务器。然后使用 FTPclient 模块。
    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多