2013-05-19 116 views
0

我想實現既FTP服務器和客戶端傳輸文件,但我基本上無法使用以Python中ftplib模塊兩者之間的連接....FTP服務器客戶端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() 

回答

0

標準庫中的ftplib模塊只覆蓋了FTP協議的客戶端部分。 所以你所做的是試圖打開兩個連接到121.0.0.1。我假設你的意思是localhost,無論如何是127.0.0.1

不知道什麼你實際上是試圖實現,還有一些選項:使用HTTP,使用(在python3或python3 -m http.serverpython -m SimpleHTTPServer

更好
+0

我會用一個行FTP服務器在python中。然後使用FTPclient模塊。 –