0% found this document useful (0 votes)
45 views

FTP Server

FTP is a standard network protocol used to transfer files between a client and server. An FTP server is created using Python that allows uploading and downloading files from a specified folder. The server operations like connections, logins etc. are logged. The server is accessed using an FTP client app on a mobile by connecting it to the same network.

Uploaded by

2021ucs0103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

FTP Server

FTP is a standard network protocol used to transfer files between a client and server. An FTP server is created using Python that allows uploading and downloading files from a specified folder. The server operations like connections, logins etc. are logged. The server is accessed using an FTP client app on a mobile by connecting it to the same network.

Uploaded by

2021ucs0103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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 pyftpdlib.authorizers import DummyAuthorizer


from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

from pyftpdlib.authorizers import DummyAuthorizer - allows you to define user


permissions for an FTP server without requiring a real authentication system.

from pyftpdlib.handlers import FTPHandler - defines the behavior of the FTP


server.For example, you can override methods like on_connect, on_disconnect,
on_login, etc., to perform custom actions when specific events occur.

from pyftpdlib.servers 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
handler.authorizer = authorizer

# Specify the address and port for the FTP server to listen on
address = ("10.10.100.175", 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 14:36:38] concurrency model: async
[I 2024-03-05 14:36:38] masquerade (NAT) address: None
[I 2024-03-05 14:36:38] passive ports: None
[I 2024-03-05 14:36:38] >>> starting FTP server on 10.10.100.175:22, pid=24464 <<<
[I 2024-03-05 14:37:04] 10.10.100.175:51430-[] FTP session opened (connect)
[I 2024-03-05 14:37:04] 10.10.100.175:51430-[] FTP session closed (disconnect).
Connected to 10.10.100.175
Disconnected from 10.10.100.175
[I 2024-03-05 14:37:07] 10.10.100.175:51432-[] FTP session opened (connect)
Connected to 10.10.100.175
[I 2024-03-05 14:37:08] 10.10.100.175:51432-[abc] USER 'abc' logged in.
[I 2024-03-05 14:37:08] 10.10.100.175:51432-[abc] CWD C:\Users\C3I ADMIN\Desktop\ftp_served 250
User abc logged in
[I 2024-03-05 14:42:08] 10.10.100.175:51432-[abc] Control connection timed out.
[I 2024-03-05 14:42:08] 10.10.100.175:51432-[abc] FTP session closed (disconnect).
Disconnected from 10.10.100.175

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

You might also like