Comments

Latest Posts

One Python Command to Start FTP or HTTP Service

It is quite easy to set up a simple ftp server or http server on your linux machine to server your simple task when you needed, mostly will be for file transferring. With python's module, pyftpdlib, SimpleHTTPServer or http.server, we can use just one command to start ftp service or http service.



    Set Up Environmet

    Because this python component is not available by default, it must be installed first.

    For Ubuntu 18.04 with Python2
     

    [email protected]:~# uname -a
    Linux ubuntu18-test1 5.4.0-1029-gcp #31~18.04.1-Ubuntu SMP Thu Oct 22 09:43:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

    Just in case, Python and PIP has not been installed yet. Here are all what you need:
    apt install python
    apt install python-pip
    pip install pyftpdlib

    If it is Ubuntu 20.04, Pip3
    apt install python3  // it might be already installed
    apt install python3-pip
    pip3 install pyftpdlib



    For CentOS 8 with Python3:

    yum install python3
    pip3 install pyftpdlib

    Here is CentOS8 installation outputs:
    [[email protected] ~]# yum install python3
    Failed loading plugin "osmsplugin": No module named 'librepo'
    Last metadata expiration check: 1:33:01 ago on Wed 05 May 2021 05:23:38 PM GMT.
    Dependencies resolved.
    =========================================================================================================================================================================================================================================
     Package                                                  Architecture                                 Version                                                                     Repository                                       Size
    =========================================================================================================================================================================================================================================
    Installing:
     python36                                                 x86_64                                       3.6.8-2.module_el8.3.0+562+e162826a                                         appstream                                        19 k
    Installing dependencies:
     python3-pip                                              noarch                                       9.0.3-18.el8                                                                appstream                                        20 k
     python3-setuptools                                       noarch                                       39.2.0-6.el8                                                                baseos                                          163 k
    Enabling module streams:
     python36                                                                                              3.6
    
    Transaction Summary
    =========================================================================================================================================================================================================================================
    Install  3 Packages
    
    Total download size: 201 k
    Installed size: 466 k
    Is this ok [y/N]: y
    Downloading Packages:
    (1/3): python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64.rpm                                                                                                                                           346 kB/s |  19 kB     00:00
    (2/3): python3-pip-9.0.3-18.el8.noarch.rpm                                                                                                                                                               356 kB/s |  20 kB     00:00
    (3/3): python3-setuptools-39.2.0-6.el8.noarch.rpm                                                                                                                                                        1.5 MB/s | 163 kB     00:00
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                                                                                                                                                                    663 kB/s | 201 kB     00:00
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Preparing        :                                                                                                                                                                                                                 1/1
      Installing       : python3-setuptools-39.2.0-6.el8.noarch                                                                                                                                                                          1/3
      Installing       : python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64                                                                                                                                                             2/3
      Running scriptlet: python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64                                                                                                                                                             2/3
      Installing       : python3-pip-9.0.3-18.el8.noarch                                                                                                                                                                                 3/3
      Running scriptlet: python3-pip-9.0.3-18.el8.noarch                                                                                                                                                                                 3/3
      Verifying        : python3-pip-9.0.3-18.el8.noarch                                                                                                                                                                                 1/3
      Verifying        : python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64                                                                                                                                                             2/3
      Verifying        : python3-setuptools-39.2.0-6.el8.noarch                                                                                                                                                                          3/3
    
    Installed:
      python3-pip-9.0.3-18.el8.noarch                                     python3-setuptools-39.2.0-6.el8.noarch                                     python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64
    
    Complete!
    
    [[email protected] ~]# yum install python3-pip
    Failed loading plugin "osmsplugin": No module named 'librepo'
    Last metadata expiration check: 1:37:54 ago on Wed 05 May 2021 05:23:38 PM GMT.
    Package python3-pip-9.0.3-18.el8.noarch is already installed.
    Dependencies resolved.
    Nothing to do.
    Complete!
    
    [[email protected] ~]# pip3 install pyftpdlib
    WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
    Collecting pyftpdlib
      Downloading https://files.pythonhosted.org/packages/31/61/63ef60aca6de07eba1639d9d47f3f8e29462e8bb49d6a8dce9aeff240646/pyftpdlib-1.5.6.tar.gz (188kB)
        100% |████████████████████████████████| 194kB 4.4MB/s
    Installing collected packages: pyftpdlib
      Running setup.py install for pyftpdlib ... done
    Successfully installed pyftpdlib-1.5.6
    [[email protected] ~]# python3 -m pyftpdlib
    /bin/python3: No module named pyftpd
    [[email protected] ~]# python3 -m pyftpdlib
    [I 2021-05-05 19:03:11] concurrency model: async
    [I 2021-05-05 19:03:11] masquerade (NAT) address: None
    [I 2021-05-05 19:03:11] passive ports: None
    [I 2021-05-05 19:03:11] >>> starting FTP server on 0.0.0.0:2121, pid=104383 <<<
    



    Start FTP Service


    Please move your location into the directory you want to ftp, since this command will turn the directory into the root directory, and providing anonymous access. The default port is 2121
    python -m pyftpdlib
    You can use ftp:// <server ip>:2121  to access your ftp server through in your browser or test it from command line as show below:
    [email protected]:~# ftp localhost 2121
    Connected to localhost.
    220 pyftpdlib 1.5.6 ready.
    Name (localhost:jon_netsec): anonymous
    331 Username ok, send password.
    Password:
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> ls
    200 Active data connection established.
    125 Data connection already open. Transfer starting.
    -rw-------   1 root     root           75 Jan 02 17:42 .bash_history
    -rw-r--r--   1 root     root         3106 Apr 09  2018 .bashrc
    drwx------   3 root     root         4096 Apr 23 01:36 .cache
    -rw-r--r--   1 root     root          148 Aug 17  2015 .profile
    drwx------   2 root     root         4096 Dec 12 20:10 .ssh
    drwxr-xr-x   3 root     root         4096 Jan 02 17:36 snap
    226 Transfer complete.
    ftp> q
    ?Ambiguous command
    ftp> quit
    221 Goodbye.


    If you need to use a username and password

    python -m pyftpdlib -u ftpuser1 -P password1234

    -u is the specified user name 

    -P is the specified password (the P is uppercase). 

    The meaning of this sentence is: the user name is ftpuser1, and the password is password1234


    Put FTP into Background to Run

    [email protected]:~#screen -S s1 python -m pyftpdlib -u ftpuser1 -P password1234
    
    [email protected]:~# screen -ls
    There is a screen on:
            30309.pts-0.ubuntu18-test1      (04/23/21 01:37:08)     (Detached)
    1 Socket in /run/screen/S-root.
    [email protected]:~# 
    Or
    nohup python -m pyftpdlib -u ftpuser1 -P password1234 &

    After running the command in the background, if you want to delete the process and close ftp, you can execute the command:

    
    [email protected]:~# screen -s s1 python -m pyftpdlib
    [I 2021-04-23 01:38:16] concurrency model: async
    [I 2021-04-23 01:38:16] masquerade (NAT) address: None
    [I 2021-04-23 01:38:16] passive ports: None
    [I 2021-04-23 01:38:16] >>> starting FTP server on 0.0.0.0:2121, pid=30345 <<<
    [I 2021-04-23 01:38:39] 127.0.0.1:53676-[] FTP session opened (connect)
    [I 2021-04-23 01:38:44] 127.0.0.1:53676-[anonymous] USER 'anonymous' logged in.
    [I 2021-04-23 01:41:27] 127.0.0.1:53676-[anonymous] FTP session closed (disconnect).
    
    CTRL+A+D to save session into background
    "screen -r" will bring session back in front. Ctrl +C to stop the service.
    Kill specific session: screen -X -S <screen name> quit
    Kill all screen sessions: pkill screen


    Or
    ps aux|grep pyftpdlib|awk'{print $2}'|xargs kill -9| grep pyftpdlib | awk '{print $2}' | xargs kill - 9


    Start FTP Service After Booted


    Write the code running in the background into /etc/rc.local
    echo "nohup python -m pyftpdlib -u ftpuser1 -p password1234 &" >> /etc/rc.local


    Other Parameters

    In addition to the above, there are some optional parameters:

    • p designated port (default is 2121)
    • w Write permission (default is read-only)
    • d Specify the directory (default is the current directory)


    From a Windows machine, type ftp command then start to open connection to remote server's ftp port 2121:

    C:\Users\test>ftp
    ftp> open 140.238.158.173 2121
    Connected to 140.238.158.173.
    220 pyftpdlib 1.5.6 ready.
    530 Log in with USER and PASS first.
    User (140.238.158.173:(none)): anonymous
    331 Username ok, send password.
    Password:
    230 Login successful.
    ftp> quit
    221 Goodbye.
    
    C:\Users\qyan>
    

     Or from online ftp client :

    https://www.net2ftp.com


    HTTP Service


    Ubuntu with python2:
    python -m SimpleHTTPServer 80

    Put it into background to run:

    nohup python -m SimpleHTTPServer 80 &

     To stop nohup's session, using fg command then ctrl+c:

    [[email protected] ~]# fg
    nohup python3 -m http.server
    


    Of course, starting an http server can be made even more convenient by an alias in your .bashrc (or the equivalent for your OS/shell). 

     
    alias http="python -m SimpleHTTPServer"

    which lets you start a server on port 8000 for the current directory with:

    http 8000


     






    CentOS with python3:

    [[email protected] ~]# screen -S s1 python3 -m http.server
    Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
    16.3.96.23 - - [05/May/2021 19:32:53] "GET / HTTP/1.1" 200 -
    

     


    One Command for Both FTP and HTTP services



    [[email protected] ~]# nohup python3 -m http.server > http.out & nohup python3 -m pyftpdlib > ftp.out &
    [1] 6170
    [2] 6171
    [[email protected] ~]# nohup: ignoring input and redirecting stderr to stdout
    nohup: ignoring input and redirecting stderr to stdout
    
    [[email protected] ~]#
    [[email protected] ~]# fg
    nohup python3 -m pyftpdlib > ftp.out
    ^C[[email protected] ~]# fg
    nohup python3 -m http.server > http.out
    ^C
    

     


    Firewall Operation

    For Ubuntu: 

    If it is using ufw, check status of ufw using command ufw status, then disable or add related ports:

    ufw disable 
    
    ufw allow 2121/tcp
    ufw allow 8000/tcp
    

     If it is using iptables, using following command to accept all inbound traffics:

    iptables -F
    

    These changes can be saved in a file with the command iptables-save for IPv4.

    Debian/Ubuntu: iptables-save > /etc/iptables/rules.v4
    RHEL/CentOS: iptables-save > /etc/sysconfig/iptables


    For CentOS :

    In CentOS, you might want to use firewall-cmd for firewalld to modify firewalls rules or you can disable it completely.

    Disable firewall:

    [[email protected] ~]# firewall-cmd --state
    running
    [[email protected] ~]# systemctl stop firewalld
    [[email protected] ~]#
    


    Add / Remove Port 8000/tcp:
    [[email protected] ~]# firewall-cmd --list-all
    public (active)
      target: default
      icmp-block-inversion: no
      interfaces: ens3
      sources:
      services: cockpit dhcpv6-client ssh
      ports:
      protocols:
      masquerade: no
      forward-ports:
      source-ports:
      icmp-blocks:
      rich rules:
    [[email protected] ~]# firewall-cmd --zone=public --permanent --add-port 8000/tcp
    success
    [[email protected] ~]# firewall-cmd --list-all
    public (active)
      target: default
      icmp-block-inversion: no
      interfaces: ens3
      sources:
      services: cockpit dhcpv6-client ssh
      ports:
      protocols:
      masquerade: no
      forward-ports:
      source-ports:
      icmp-blocks:
      rich rules:
    [[email protected] ~]# firewall-cmd --reload
    success
    [[email protected] ~]# firewall-cmd --list-all
    public
      target: default
      icmp-block-inversion: no
      interfaces:
      sources:
      services: cockpit dhcpv6-client ssh
      ports: 8000/tcp
      protocols:
      masquerade: no
      forward-ports:
      source-ports:
      icmp-blocks:
      rich rules:
    [[email protected] ~]# firewall-cmd --zone=public --permanent --remove-port 8000/tcp
    success
    [[email protected] ~]#
    
    






    No comments