简体   繁体   中英

FTP server Client using ftplib

I am trying to implement both a FTP server and a client to transfer files, but i am basically unable to make a connection between the two using the ftplib module in python....

This is what I did, how can I fix the code? Is there another approach?

Code for my server : -

       #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

Code for my client : -

     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()

The ftplib module from the standard library only covers the client part of the FTP protocol. So what you did is trying to open two connections to 121.0.0.1 . I'm assuming you meant localhost , which is 127.0.0.1 anyway.

Not knowing what you are actually trying to achieve, there are some options:

  • transfer files using HTTP, use python -m SimpleHTTPServer (or python3 -m http.server in python3) and requests as client
  • use a prexisting solution (see the answers to One line ftp server in python )
  • implement your own server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM