This article was first published in the wechat public account “Beauty of Operation and Maintenance”, the public account ID: Hi-Linux.

“Beauty of Operation and maintenance” is a feeling, attitude, dedicated to Linux operation and maintenance related technical articles to share the public account. The public account is dedicated to sharing all kinds of technical articles and publishing the most cutting-edge scientific and technological information for the majority of operation and maintenance workers. The core concept of the official account is: sharing, we believe that only sharing can make our group stronger. If you want to be the first to get the latest technical articles, please follow us!

Mike, the author of the public account, earns 3000 yuan a month as a handyman. Engaged in IT related work for 15+ years, keen on Internet technology field, identify with open source culture, have their own unique insights on operation and maintenance related technology. I am willing to share my accumulated experience, experience and skills with you. Don’t miss the dry goods. If you want to contact me, you can follow the public account for relevant information.


Fast HTTP server implementation in Python

Sometimes you need to temporarily build a simple Web Server, but you don’t want to install Apache, Nginx, and other complex HTTP services. You can quickly set up a SimpleHTTPServer using Python’s built-in SimpleHTTPServer module.

The SimpleHTTPServer module displays the files and folders in your specified directory as a simple Web page. Suppose we need to share the directory /Users/Mike/Docker on the Web. This can be done easily with the following command line:

$ cd/Users/Mike/Docker $python -m SimpleHTTPServer Serving HTTP on 0.0.0.0 port 8000...Copy the code

The SimpleHTTPServer module listens for an HTTP service on port 8000 by default, so you can access the Web page by opening a browser and typing http://IP:Port. For example, a URL like the following:

http://192.168.100.49:8000
Copy the code

If you want the Web service to have a default page, you can create a file called index.html in the directory. If there is no default page, the contents of the directory are displayed as a list.

If the default port 8000 is already used and you want to use another port number, you can use the following command:

$ python -m SimpleHTTPServer 8080
Copy the code

Fast FTP server implementation with Python

This is especially useful when you want to quickly set up an FTP server to temporarily upload and download files. Here we use Python Pyftpdlib module can quickly implement the function of an FTP server.

First, install the Pyftpdlib module

$ sudo pip install pyftpdlib
Copy the code

Run the Pyftpdlib module as a simple standalone server with Python’s -m option. Assuming we need the shared directory /Users/Mike/Docker, this can be done easily with the following command line:

$ cd /Users/Mike/Docker
$ python -m pyftpdlib
[I 2018-01-02 16:24:02] >>> starting FTP server on :::2121, pid=7517 <<<
[I 2018-01-02 16:24:02] concurrency model: async
[I 2018-01-02 16:24:02] masquerade (NAT) address: None
[I 2018-01-02 16:24:02] passive ports: None
Copy the code

At this point, a simple FTP server has been set up. You can visit ftp://IP:PORT. For example, a URL like the following:

ftp://192.168.100.49:2121
Copy the code
  • The default IP address is all available IP addresses and port number is 2121.
  • The default login mode is anonymous.
  • The default permission is read-only.

If you want to set up a writable FTP server with authentication, you can use commands like the following:

$python -m pyftpdlib -i 192.168.100.49 -w-d /tmp/ -u mike -P 123456
Copy the code

During the test, a weak password, such as password 000000, is used as the authentication password, and the authentication failure message is displayed when the client logs in. Pyftpdlib also provides basic security policies.

Common Optional parameters:

-i Specify the IP address (default is all available IP addresses of the host) -p Specify the port (default is 2121) -w Write permission (default is read-only)-dSpecify the directory (current directory by default) -u Specify the login user name -p Specify the login passwordCopy the code

More parameters can be queried using the following command:

$ python -m pyftpdlib --help

Usage: python -m pyftpdlib [options]

Start a stand alone anonymous FTP server.

Options:
  -h, --help
     show this help message and exit

  -i ADDRESS, --interface=ADDRESS
     specify the interface to run on (default all interfaces)

  -p PORT, --port=PORT
     specify port number to run on (default 2121)

  -w, --write
     grants write access for logged in user (default read-only)

  -d FOLDER, --directory=FOLDER
     specify the directory to share (default current directory)

  -n ADDRESS, --nat-address=ADDRESS
     the NAT address to use for passive connections

  -r FROM-TO, --range=FROM-TO
     the range of TCP ports to use for passive connections (e.g. -r 8000-9000)

  -D, --debug
     enable DEBUG logging evel

  -v, --version
     print pyftpdlib version and exit

  -V, --verbose
     activate a more verbose logging

  -u USERNAME, --username=USERNAME
     specify username to login with (anonymous login will be disabled and password required if supplied)

  -P PASSWORD, --password=PASSWORD
     specify a password to login with (username required to be useful)

Copy the code

If you need to uninstall the Pyftpdlib module, you can use the following command:

$ pip uninstall pyftpdlib
Copy the code

Reference documentation

www.google.com

Coolshell. Cn/articles / 14…

Pyftpdlib. Readthedocs. IO/en/latest/I…

My.oschina.net/kangvcar/bl…