LoginSignup
41
41

More than 5 years have passed since last update.

すぐ作ってすぐ壊せるFTPサーバーを立てる (Pythonで)

Posted at

pythonにpyftpdlibという便利そうなFTPサーバーライブラリがあったので使ってみました。

pyftpdlibをインストールする

pyftpdlibのサイトからダウンロードできるようですが・・・
pipを使うとコマンド1つでインストールできるのでおすすめ。

pip install pyftpdlib

ついでに、PyCharmを使えばGUIを使ってpipの実行ができます!(宣伝)
rapture_20140323171404.png

簡単なFTPサーバーを作る

基本は公式サイトのクイックスタートの通り。

自分はWindows環境でやりましたが、他のOSでもできると思います

# -*- coding: utf8 -*-
import pyftpdlib.authorizers
import pyftpdlib.handlers
import pyftpdlib.servers

# 認証ユーザーを作る
authorizer = pyftpdlib.authorizers.DummyAuthorizer()
authorizer.add_user('user', 'password', 'C:\\Users\\username\\Pictures', perm='elradfmw')

# 個々の接続を管理するハンドラーを作る
handler = pyftpdlib.handlers.FTPHandler
handler.authorizer = authorizer

# FTPサーバーを立ち上げる
server = pyftpdlib.servers.FTPServer(("127.0.0.1", 21), handler)
server.serve_forever()

この例だと、C:\Users\username\Picturesがルート・ディレクトリとなったFTPサーバーが立ち上がります。

ユーザー名:user, パスワード:password
でログインできます。

試しに、WinSCPでつないでみると、ちゃんと画像フォルダの中身が表示されました。

その他いろいろな拡張

まだ自分は見ていませんが、公式サイトのチュートリアルに様々な例が載っているようです。

41
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
41