Skip to content

Instantly share code, notes, and snippets.

@ekoew
Last active July 4, 2018 11:01
Show Gist options
  • Save ekoew/aa585d5270c6296edb7402e2d6fc2e35 to your computer and use it in GitHub Desktop.
Save ekoew/aa585d5270c6296edb7402e2d6fc2e35 to your computer and use it in GitHub Desktop.
School project by Eko Puji Pramono (5115100065) based on tutorial: http://www.techinfected.net/2017/07/create-simple-ftp-server-client-in-python.html
# download for anonymous user
from ftplib import FTP
ftp = FTP('')
ftp.connect('localhost',1026) # connect to host, default port
ftp.login() # login as anonymous user
ftp.cwd('anonymous') # change into "files" directory
print ("Permissions No. Owner Type Size Date Time Filename")
ftp.retrlines('LIST') # retrieve list of file in the directory
def downloadFile():
filename = raw_input('Enter filename to download: ')
localfile = open(filename, 'wb')
ftp.retrbinary('RETR ' + filename, localfile.write, 1024) # retrieve/download the file from the directory
ftp.quit() # quit and close the connection
localfile.close()
downloadFile() # call the downloadFile function
# upload for anonymous user
from ftplib import FTP
ftp = FTP('')
ftp.connect('localhost',1026) # connect to host, default port
ftp.login() # login as anonymous user
ftp.cwd('anonymous') # change into "files" directory
def uploadFile():
filename = raw_input('Enter filename to upload: ')
ftp.storbinary('STOR '+filename, open(filename, 'rb')) # store/upload the file to the directory
ftp.quit() # quit and close the connection
uploadFile() # call the uploadFile function
#pertama harus install library pyftpdlib dengan menjalankan command pada cmd: python -m pip install pyftpdlib
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
# Instantiate a dummy authorizer for managing 'virtual' users
authorizer = DummyAuthorizer()
# Define a new user having full r/w permissions
#authorizer.add_user("user", "12345", "home/username", perm="elradfmw")
authorizer.add_anonymous("home/username", perm="elradfmw")
# Instantiate FTP handler class
handler = FTPHandler
handler.authorizer = authorizer
# Instantiate FTP server class and listen on 127.0.0.1:1026
server = FTPServer(("127.0.0.1", 1026), handler)
# start ftp server
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment