Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ftplib
- def connect_ftp(server, username, password):
- """
- Membuat koneksi ke server FTP dan login.
- """
- try:
- ftp = ftplib.FTP(server)
- ftp.login(user=username, passwd=password)
- print(f"Berhasil terhubung ke {server}")
- return ftp
- except ftplib.error_perm as e:
- print(f"Login gagal: {e}")
- exit(1)
- except Exception as e:
- print(f"Kesalahan koneksi: {e}")
- exit(1)
- def list_directory(ftp):
- """
- Menampilkan daftar file di direktori server FTP.
- """
- try:
- print("Daftar file di direktori server:")
- ftp.retrlines("LIST")
- except Exception as e:
- print(f"Kesalahan saat membaca direktori: {e}")
- def upload_file(ftp, local_file, remote_file):
- """
- Mengunggah file ke server FTP.
- """
- try:
- with open(local_file, "rb") as f:
- ftp.storbinary(f"STOR {remote_file}", f)
- print(f"File {local_file} berhasil diunggah sebagai {remote_file}.")
- except Exception as e:
- print(f"Kesalahan saat mengunggah file: {e}")
- def download_file(ftp, remote_file, local_file):
- """
- Mengunduh file dari server FTP.
- """
- try:
- with open(local_file, "wb") as f:
- ftp.retrbinary(f"RETR {remote_file}", f.write)
- print(f"File {remote_file} berhasil diunduh sebagai {local_file}.")
- except Exception as e:
- print(f"Kesalahan saat mengunduh file: {e}")
- def main():
- server = "192.168.56.105"
- username = "anehinnn"
- password = "1234"
- ftp = connect_ftp(server, username, password)
- try:
- list_directory(ftp)
- upload_file(ftp, "220010178_ibnu.txt", "ibnu_upload.txt")
- download_file(ftp, "ibnu_upload.txt", "local_download.txt")
- list_directory(ftp)
- finally:
- ftp.quit()
- print("Koneksi FTP ditutup.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement