この記事では、ファイル転送プロトコルとは何かについて学び、ファイル転送プロトコルの機能をPythonで様々な手法で実装していきます。
ファイル転送プロトコル(FTP)とは何ですか?
FTPは、ネットワーク上の2つのシステム間でファイル転送を行うためのネットワークプロトコルです。
クライアント・サーバ・アーキテクチャとして動作し、クライアントシステムは認証の有無にかかわらずサーバシステムに接続でき、適切な権限が与えられていれば、クライアントはサーバマシンからファイルを表示、取得、削除することができます。
Python で File Transfer Protocol を使ってサーバに接続する方法は?
様々なタスクを実行するためには、まずftplibモジュールをインポートする必要があります。
その後、モジュール内の関数を使用して機能を実装します。
以下のコードでは、ftplibモジュールの FTP() 関数を使って ftp.ubuntu.com に接続し、 login() メソッドを使ってサイトにログインしています。
ログイン後、接続が確立されると、サーバーはウェルカムメッセージを返し、それは getwelcome() メソッドを使用して表示することができます。
#import moduleimport ftplib
#define server ftp addresssite_addr= "ftp.ubuntu.com"
#make a connection to serverftp_obj = ftplib.FTP(site_addr)
#login to the serverftp_obj.login()print("Connected to "+ site_addr +". Welcome message is:")
#print the welcome messageprint(ftp_obj.getwelcome())
#close the connectionftp_obj.close() |
結果を出力すると、以下の様になります。
結果は以下の通りです。
Connected to ftp.ubuntu.com. Welcome message is:
220 FTP server (vsftpd)
|
プログラムの最後には close() メソッドを使って接続を閉じる必要があることに注意してください。
この記事もチェック:PythonのOshashモジュールを使ってハッシュ関数を実装する方法
現在の作業ディレクトリの名前を表示する
サーバーにログインした後、pwd() メソッドを使用して現在の作業ディレクトリの名前を表示することができます。
このメソッドは FTP() 関数が返すオブジェクトに対して実行され、現在の作業ディレクトリへの絶対パスを返します。
#import moduleimport ftplib
#define server ftp addresssite_addr= "ftp.ubuntu.com"
#make a connection to serverftp_obj = ftplib.FTP(site_addr)
#login to the serverftp_obj.login()print("Present Working Directory is:")
#get the name of present working directorypresent=ftp_obj.pwd()
print(present)
#close the connectionftp_obj.close() |
結果は以下の通りです。
出力:
Present Working Directory is:
/ |
ディレクトリの中身を印刷する
FTP()関数が返すオブジェクトに対してdir()メソッドを使用すると、現在の作業ディレクトリの内容を表示することができます。
dir()メソッドは単に現在の作業ディレクトリの内容を表示するだけで、何も返しません。
#import moduleimport ftplib
#define server ftp addresssite_addr= "ftp.ubuntu.com"
#make a connection to serverftp_obj = ftplib.FTP(site_addr)
#login to the serverftp_obj.login()print("Content of the directory "+ " is:")
#print the content of present working directoryftp_obj.dir()
#close the connectionftp_obj.close() |
結果は以下の通りです。
Content of the directory is:
drwxr-xr-x 31 997 997 4096 Dec 24 17:47 cdimage
drwxr-xr-x 28 997 997 4096 Dec 24 18:14 cloud-images
drwxr-xr-x 8 997 997 4096 Dec 18 21:56 maas-images
drwxr-xr-x 5 997 997 4096 May 11 2010 old-images
drwxr-xr-x 13 997 997 4096 Dec 24 17:01 releases
drwxr-xr-x 6 997 997 4096 Dec 24 18:19 simple-streams
drwxr-xr-x 7 997 997 4096 Dec 24 17:54 ubuntu
drwxr-xr-x 4 997 997 4096 Oct 01 01:33 ubuntu-cloud-archive
drwxr-xr-x 7 997 997 4096 Dec 24 18:21 ubuntu-ports
|
作業ディレクトリの変更
FTP()関数が返すオブジェクトに対してcwd()メソッドを呼び出すことで、現在の作業ディレクトリを変更することができます。
新しいディレクトリの相対パスはcwd()` メソッドへの入力として与えられ、このメソッドは何も返しません。
メッセージを表示するために print() 関数を使用します。
#import moduleimport ftplib
#define server ftp addresssite_addr= "ftp.ubuntu.com"
#make a connection to serverftp_obj = ftplib.FTP(site_addr)
#login to the serverftp_obj.login()print("Present Working Directory is:")
#get the name of present working directorypresent=ftp_obj.pwd()
print(present)
print("Content of the directory "+ " is:")
#print the content of present working directoryftp_obj.dir()
#change present working directory to "ubuntu"ftp_obj.cwd("ubuntu")
print("After Change in directory:")
print("Present Working Directory is:")
#get the name of present working directorypresent=ftp_obj.pwd()
print(present)
print("Content of the directory "+ " is:")
#print the content of present working directoryftp_obj.dir()
#close the connectionftp_obj.close() |
結果は以下の通りです。
Present Working Directory is:
/Content of the directory is:
drwxr-xr-x 31 997 997 4096 Dec 24 17:47 cdimage
drwxr-xr-x 28 997 997 4096 Dec 24 18:14 cloud-images
drwxr-xr-x 8 997 997 4096 Dec 18 21:56 maas-images
drwxr-xr-x 5 997 997 4096 May 11 2010 old-images
drwxr-xr-x 13 997 997 4096 Dec 24 17:01 releases
drwxr-xr-x 6 997 997 4096 Dec 24 18:24 simple-streams
drwxr-xr-x 7 997 997 4096 Dec 24 17:54 ubuntu
drwxr-xr-x 4 997 997 4096 Oct 01 01:33 ubuntu-cloud-archive
drwxr-xr-x 7 997 997 4096 Dec 24 18:21 ubuntu-ports
After Change in directory:
Present Working Directory is:
/ubuntu
Content of the directory is:
drwxrwxr-x 37 997 997 4096 Oct 23 11:04 dists
drwxr-xr-x 2 997 997 192512 Dec 24 17:44 indices
-rw-r--r-- 1 997 997 20997733 Dec 24 17:44 ls-lR.gz
drwxrwxr-x 6 997 997 4096 Feb 27 2010 pool
drwxr-xr-x 3 997 997 4096 Jun 28 2013 project
lrwxrwxrwx 1 997 997 1 Nov 24 2010 ubuntu -> .
|
ファイルサイズを確認する
ファイルのサイズをチェックするには、ftplib モジュールの size() メソッドを使用します。
size()メソッドはFTP()` 関数が返すオブジェクトに対して呼び出され、ファイルへのパスがメソッドへの入力として与えられます。
ファイルサイズはバイト単位で出力されます。
#import moduleimport ftplib
#define server ftp addresssite_addr= "ftp.ubuntu.com"
#make a connection to serverftp_obj = ftplib.FTP(site_addr)
#login to the serverftp_obj.login()ftp_obj.cwd("ubuntu")
print("Present Working Directory is:")
#get the name of present working directorypresent=ftp_obj.pwd()
print(present)
print("Content of the directory "+ " is:")
ftp_obj.dir()
#print size of "ls-lR.gz"fsize= ftp_obj.size("ls-lR.gz")
print("Size of file ls-lR.gz is:"+ str(fsize))
#close the connectionftp_obj.close() |
結果は以下の通りです。
出力:
Present Working Directory is:
/ubuntu
Content of the directory is:
drwxrwxr-x 37 997 997 4096 Oct 23 11:04 dists
drwxr-xr-x 2 997 997 180224 Dec 24 18:43 indices
-rw-r--r-- 1 997 997 20988844 Dec 24 18:43 ls-lR.gz
drwxrwxr-x 6 997 997 4096 Feb 27 2010 pool
drwxr-xr-x 3 997 997 4096 Jun 28 2013 project
lrwxrwxrwx 1 997 997 1 Nov 24 2010 ubuntu -> .
Size of file ls-lR.gz is:20988844
|
まとめ
この記事では、Python で ftplib モジュールを使ってファイル転送プロトコルを操作し、リモートサーバーのファイルに対して読み取り操作を行う方法について見てきました。
今後も有益なチュートリアルをお届けする予定です。
それでは、よいお年を