Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build FTP server in Linux environment and how to realize the interaction of FTP client in Python?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to build FTP server in Linux environment and how to realize the interaction of FTP client in Python? this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

FTP is the abbreviation of File Transfer Protocol (File transfer Protocol). It is based on the transport layer protocol TCP and is used for two-way transfer (upload and download) of files on Internet. Like most Internet services, FTP is a client / server system, and the cooperation of the FTP server and the FTP client is needed to complete the file transfer.

The FTP protocol uses two TCP connections, a command link for passing commands between the FTP client and the server, and a data link for uploading or downloading data. The FTP server / client architecture is shown in the figure:

FTP protocol works in two ways: PORT active mode and PASV passive mode. Regardless of the mode of operation, the client is required to actively establish a command link with the FTP server on the remote host.

PORT connection process: the client sends a connection request from an arbitrary unprivileged port M (M > 1023 to 1023 to bind a specific service) to the command port of the FTP server (the default is 21), and the server accepts the connection and establishes a command link. When data needs to be transferred, the client sends the FTP command "port masking 1" on the command chain to the FTP server. Then the client starts listening on port Mroom1, and the server connects from its own data port (default is 20) to the data port specified by the client (Mumb1) to establish a data link to transmit data.

In PASV mode, both command and data connections are initiated by the client, and when a FTP connection is opened, the client opens two arbitrary unprivileged local ports (M > 1023 and M + 1). The first port connects to the server's port 21 to establish a command link in the same way as PORT, but the second port establishes a data link in a different way than PORT. When data needs to be transferred, the client submits the PASV command to the server, and as a result, the server opens an arbitrary unprivileged port (N > 1023) and sends the PASV N command to the client. So the client initiates a connection from local port Mumb1 to port N of the server and establishes a data link to transmit data.

Because you must log in to transfer files using FTP, you can upload and download files only after you have obtained the appropriate permissions on the remote host. The file cannot be transferred unless you have a user ID and password. There are thousands of FTP hosts on Internet, and it is impossible to require every user to have an account on every host, which violates the openness of Internet. So anonymous FTP is created to solve this problem.

Through the anonymous FTP mechanism, users can connect to the remote host to upload and download files without registering an account. The system administrator has established a special user ID called anonymous, which can be used anywhere by anyone on the Internet.

Build FTP server on Linux platform

Generally, in various Linux distributions, the default FTP software is vsftp. Vsftpd (Very Secure FTP Daemon) is a FTP server program that runs on a Unix-like operating system. Here, vsftp is used to build a FTP server on the ubuntu distribution of the Linux platform, which can be tested with the client of the Python implementation.

(1) use the # rpm-qa | grep vsftpd command to check whether vsftpd software is installed. If not, use the # sudo apt-get install vsftpd command to install vsftpd software.

(2) use the # netstat-tnl command to check whether the port service is open. The FTP server opens port 21 by default.

(3) use the command # service vsftpd start | stop | restart to start, stop, and restart FTP.

(4) the configuration file of FTP is located in the / etc/vsftpd/ directory, including the user prohibited login list ftpusers and user_list, and the main configuration file vsftpd.conf. Use the # sudo gedit / etc/vsftpd.conf command to modify the configuration file vsftpd.conf, remove the # before the configuration item to enable the configuration, and restart the service after modifying the configuration file.

The configurations related to anonymous login are as follows:

Anonymous_enable=YES # controls whether anonymous users are allowed to log in. YES allows anonymous login and NO disallows it. The default is YES.

Whether write_enable=YES # allows login users to have write access. It is a global setting, and the default value is YES.

No_anon_password=YES # if you start this feature, you will not ask for a password when you log in anonymously. The default is NO.

Ftp_username=ftp# defines the user name for anonymous login. The default is ftp.

Anon_root=/var/ftp

# the directory logged in when you log in anonymously. The default is / var/ftp. Note that the ftp directory cannot be a permission attribute of 777.

Anon_upload_enable=YES

# if set to YES, anonymous logins are allowed to upload files (not directories), which is valid only when write_enable=YES. The premise is that anonymous users must have the right to write to the upper directory. The default is NO.

Anon_world_readable_only=YES

# if set to YES, anonymous logins are allowed to download readable files (which can be downloaded to the local machine and cannot be opened directly in the FTP server). The default is YES.

Anon_mkdir_write_enable=YES

# if set to YES, anonymous logins are allowed to add directories, which is valid only when write_enable=YES is used. The premise is that anonymous users must have the right to write to the upper directory. The default is NO.

Anon_other_write_enable=YES

# if set to YES, more anonymous logins are allowed to upload or create permissions outside the directory, such as deletion or renaming. (if anon_upload_enable=NO, anonymous users cannot

Upload files, but delete or rename existing files; if anon_mkdir_write_enable=NO, anonymous users cannot upload or create new folders, but can delete or rename existing folders. The default value is NO.

Chown_uploads=YES # sets whether to change the owner of files (not directories) uploaded by anonymous users. The default is NO.

Chown_username=username # sets the owner name of the file (not a directory) uploaded by anonymous users. It is not recommended to set it to root.

Anon_umask=022

# set the mask value for anonymous logins when adding or uploading files. The default value is 077. For example, if umask is 022, create a file with permissions of 666. the actual permissions of the file are 666-022 permissions 644.

Deny_email_enable=YES

# if you start this feature, you must provide a file / etc/vsftpd/banner_emails with the content email address. If you log in anonymously, you will be asked to enter email address, and if you enter email address

Access is not allowed in this file. The default is NO.

Banned_email_file=/etc/vsftpd/banner_emails # this file is used to enter email

Address, this file is only used when deny_email_enable=YES. If you use anonymous login, you will be asked to enter email

Address, if the email address entered is in this file, it is not allowed to enter.

Other major configurations are as follows:

Listen= NO # server listens, whether to monitor the service in an independent way

Whether local_enable=YES # allows local users to access

Local file permissions on local_umask=022 # FTP. Default is 077

# dirmessage_enable=YES # allowed to enter the folder

Xferlog_enable=YES # ftp logging allows

Onnect_from_port_20=YES # enables port 20 as a port for data transfer

Xferlog_enable=yes # activate uploaded and downloaded logs

# xferlog_std_format=yes # use standard log format

# ftpd_banner=XXXXX # Welcome message

Local_root=/var/www # the directory you are logged in when you log in locally.

Pasv_enable=YES #

If set to YES, PASV working mode is used; if set to NO, PORT mode is used. The default value is YES, even if you work in PASV mode.

Pasv_min_port=30000 # in PASV mode, the maximum port in the range of ports that a data connection can use, with 0 representing any port. The default value is 0.

Pasv_max_port=30999 # in PASV mode, the smallest port in the range of ports that a data connection can use, with 0 representing any port. The default value is 0.

(5) use the # sudo userdel uftp command to delete the user, and use the # sudo apt-get remove-- purge vsftpd command to completely delete the software and related files

(6) the permission setting of anonymous login directory. By default, the root directory for anonymous login is / var/ftp (actually configured as / home/ftp). For security, this directory is not allowed to be set to 777 permissions by default, otherwise ftp will not be able to access it anonymously. When we want to upload files anonymously, we need to set write permissions for other users.

Sudo chmod otakw / home/ftp

Change the primary permissions of the ftp directory from the default root permissions to anonymous ftp permissions. The parameter-R starts recursive processing and modifies the permissions of all files in a directory, including the file permissions in the subdirectory. The modified ftp account can access the ftp folder.

Sudo chown ftp / home/ftp-R

Sudo ls-l / home/

Total dosage 12

Drwxr-xr-x 2 ftp root 4096 February 15 19:09 ftp

Drwxr-xr-x 2 uftp root 4096 February 15 16:23 uftp

Drwxr-xr-x 40 yuanxiao yuanxiao 4096 February 15 19:22 yuanxiao

You can also use vsftp to build a FTP server on the embedded Linux platform LED distribution.

(1) connect the development board to the external network and install vsftp through the opkg command.

Root@Yuanxiao:/etc/init.d# opkg update

Root@Yuanxiao:/etc/init.d# opkg install vsftpd

(2) configure / etc/vsftpd.conf file. The meaning and method of configuration options are the same as those of vsftpd on the above ubuntu distribution. If you log in anonymously, you need to set the corresponding options for the / etc/vsftpd.conf file.

(3) change the permissions of / etc/vsftpd.conf file.

An error is prompted when starting the vsftpd process, indicating that the configuration file does not belong to the correct user.

Root@Yuanxiao:~# / etc/init.d/vsftpd start

500 OOPS: config file not owned by correct user, or not a file

The workaround is to set up the access user of the / etc/vsftpd.conf file (currently a root user)

Root@Yuanxiao:~# chown root / etc/vsftpd.conf

Root@Yuanxiao:~# ls-l / etc/vsftpd.conf

-rwxrwxrwx 1 root root 840 Feb 15 13:48 / etc/vsftpd.conf

(4) start and stop vsftpd.

/ etc/init.d/vsftpd start

/ etc/init.d/vsftpd stop

Example of client-side FTP program based on Python

#-*-coding: utf-8-*-# python 27from ftplib import FTPftp = FTP () # create FTP object timeout = 30 port = 21ftp.connect ('192.168.1.188) # Connect to FTP server ftp.login (' UserName' '888888') # Log in to print ftp.getwelcome () # get welcome information ftp.cwd ('file/test') # set the current path under the FTP remote directory list = ftp.nlst () # get the list of files in the directory for name in list: print name# downloads files from the FTP server buffer size filename= "/ usr/share/app/uboot.bin" # needs to download And the local location file_handle=open (filename) "wb"). Write # opens the file ftp.retrbinary ("RETR uboot.bin", file_handle) locally in write mode Bufsize) # download files on the server and write to local files # Delete remote files ftp.delete ("art.bin") # Delete FTP server files # upload files to FTP server buffer size filename= "/ usr/share/app/main.py" # files to be uploaded and local storage location file_handle=open (filename, "rb") # Open the file ftp.storbinary locally in read mode ("STOR main.py") File_handle,bufsize) # upload local files to FTP server ftp.quit () # exit FTP server questions about how to build a FTP server in Linux environment and how Python can interact with FTP clients are shared here I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report