What is FTP (File Transfer Protocol)
FTP, or File Transfer Protocol, is a standard network protocol used to transfer files between a client
and a server on a computer network. It is one of the oldest and most widely used protocols for file
transfer over a TCP-based network, such as the internet or an intranet.
Here's a breakdown of its key components:
1) File Transfer:
FTP enables the transfer of files from one host to another. It allows users to upload files from their
local machines to a remote server and download files from a remote server to their local
machines.
2) Client-Server Model:
FTP operates on a client-server model where one machine acts as the server, and others act as
clients. The server hosts the files and responds to requests from clients, which initiate file
transfers.
3) Connection Types:
FTP supports two types of connections:
Control Connection: Used for sending commands and receiving responses. It remains open during
the entire session.
Data Connection: Used for transferring actual data files. It is opened and closed for each file
transfer.
4) Commands and Responses:
FTP uses a set of commands that clients send to the server to perform various actions. The server
responds with status codes indicating the success or failure of the command.
5) Authentication:
Users need to authenticate themselves with a valid username and password to access files on an
FTP server. This ensures secure access and prevents unauthorized file transfers.
6) Modes of Operation:
FTP operates in two modes:
Active Mode: The client opens a random port, and the server connects to it for data transfer.
Passive Mode: The server opens a random port, and the client connects to it for data transfer.
Passive mode is often used to overcome issues with firewalls and NAT.
7) Security Considerations:
While traditional FTP operates in plaintext, security extensions like FTPS (FTP Secure) and SFTP
(SSH File Transfer Protocol) provide encryption and secure authentication.
Now we will create a FTP server on our machine using python
( Note : For a better understanding, we will download server application later)
Install pyftpdlib package on your machine
In [1]: pip install pyftpdlib
Requirement already satisfied: pyftpdlib in c:\users\c3i admin\anaconda3\lib\site-packages (1.5.9)
Note: you may need to restart the kernel to use updated packages.
In [1]: from [Link] import DummyAuthorizer
from [Link] import FTPHandler
from [Link] import FTPServer
from [Link] import DummyAuthorizer - allows you to define user
permissions for an FTP server without requiring a real authentication system.
from [Link] import FTPHandler - defines the behavior of the FTP
[Link] example, you can override methods like on_connect, on_disconnect,
on_login, etc., to perform custom actions when specific events occur.
from [Link] import FTPServer - It is responsible for managing the
server's connections, accepting incoming connections, and dispatching them to
the appropriate handler.'''
Authorizer
An "authorizer" is a component responsible for managing user authentication and
determining their permissions within an FTP server. The DummyAuthorizer class
in pyftpdlib is one implementation of an authorizer.
In [2]: class MyHandler(FTPHandler):
def on_connect(self):
# Called when a client connects
print(f"Connected to {self.remote_ip}")
def on_disconnect(self):
# Called when a client disconnects
print(f"Disconnected from {self.remote_ip}")
def on_login(self, username):
# Called when a user logs in
print(f"User {username} logged in")
def on_logout(self, username):
# Called when a user logs out
print(f"User {username} logged out")
def on_file_sent(self, file):
# Called when a file has been successfully sent to the client
print(f"File {file} sent successfully")
def on_file_received(self, file):
# Called when a file has been successfully received from the client
print(f"File {file} received successfully")
def on_incomplete_file_sent(self, file):
# Called when a file has been partially sent
print(f"Incomplete file {file} sent")
def on_incomplete_file_received(self, file):
# Called when a file has been partially received
print(f"Incomplete file {file} received")
In [ ]: def run_ftp_server():
# Instantiate an authorizer
authorizer = DummyAuthorizer()
# Add a user with read/write permissions
print("Enter username")
username=input()
print("Enter password in numeric form")
password=int(input())
print("Enter the path of your file")
path=input() # make a folder anywhere on your device put some files,vid,images anything in it and give that path here
authorizer.add_user(username, password, path, perm="elradfmw")
#note about perm field below
# Instantiate an FTP handler and link it to the authorizer
handler = MyHandler
[Link] = authorizer
# Specify the address and port for the FTP server to listen on
address = ("[Link]", 22)
# Instantiate and start the FTP server
server = FTPServer(address, handler)
server.serve_forever()
if __name__ == "__main__":
run_ftp_server()
Enter username
abc
Enter password in numeric form
1234
Enter the path of your file
C:/Users/C3I ADMIN/Desktop/ftp_served
[I 2024-03-05 [Link] concurrency model: async
[I 2024-03-05 [Link] masquerade (NAT) address: None
[I 2024-03-05 [Link] passive ports: None
[I 2024-03-05 [Link] >>> starting FTP server on [Link]:22, pid=24464 <<<
[I 2024-03-05 [Link] [Link]:51430-[] FTP session opened (connect)
[I 2024-03-05 [Link] [Link]:51430-[] FTP session closed (disconnect).
Connected to [Link]
Disconnected from [Link]
[I 2024-03-05 [Link] [Link]:51432-[] FTP session opened (connect)
Connected to [Link]
[I 2024-03-05 [Link] [Link]:51432-[abc] USER 'abc' logged in.
[I 2024-03-05 [Link] [Link]:51432-[abc] CWD C:\Users\C3I ADMIN\Desktop\ftp_served 250
User abc logged in
[I 2024-03-05 [Link] [Link]:51432-[abc] Control connection timed out.
[I 2024-03-05 [Link] [Link]:51432-[abc] FTP session closed (disconnect).
Disconnected from [Link]
Note : the string "elradfmw" represents the permissions assigned to
a user. Each character in the string corresponds to a specific
permission:
e: Change directory (CWD)
l: List files (LIST)
r: Retrieve a file from the server (RETR)
a: Append data to an existing file (APPE)
d: Delete a file or directory (DELE, RMD)
f: Make directory (MKD)
m: Change data channel protection level (PROT)
w: Store a file on the server (STOR)
So, in the context of the line:
authorizer.add_user(username, password, path, perm="elradfmw")
This user ("username") is granted the above permissions
Now download any FTP manager which can host this FTP server
(for this we can use cyberduck for its user friendly interface)
After installing cyberduck, go on 'open connection' and fill the details
The above code is for the same system but what if we have to see
the file content on other device for example in your phone ?
For that download "AndFTP" for Android or "FTPManager" for iOS.
Now connect your PC with your Mobile Hotspot.
Note that you have to put the wifi IP address in your code in "Address" field or otherwise it will not work.
Now open the app and add new connection and enter the details.
The contents of the server will now be displayed on your phone.
Bingo ! you made your own file sharing app
In [ ]: C:/Users/C3I ADMIN/Desktop/ftp_served