Last active
July 4, 2018 11:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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