Introduction to Python utility widgets

Source: Internet
Author: User

One, Second level start an HTTP download server
In practice, there is a need to pass documents to other colleagues. It is not a tedious task to pass a file to a colleague, and now the Chat tool generally supports file transfer. However, if you need to transfer more files, then the operation will be more troublesome. In addition, if the file is on a remote server and you want to pass the file on to a colleague, you need to download the file from the remote server locally before passing it to your colleague via the chat tool. Or, you are not particularly clear about which documents to send to colleagues, so you need to communicate back and forth. The time cost of communication is relatively high, it will reduce the efficiency. At this point, you need a more efficient approach. This time, if you know that Python has a built-in download server that can significantly improve efficiency. For example, if your colleague wants your file to be in a directory, you can enter the directory and then execute the following command to start a download server:
There is a local folder, want to share to LAN colleagues to download some of the files inside, you can use the following Python command.
? The usage of Python2 is as follows:
Python-m Simplehttpserver
? The usage of Python3 is as follows:
Python3-m Http.server--cgi
The above two methods default port 8000, you can develop a port, for example, specify Port 45678:
Python-m Simplehttpserver 45678
Python3-m Http.server--cgi 45678

If the native IP address is 10.10.40.112, the same network segment others in the browser input 10.10.40.112:45678 access to the shared folder content

Note: From a working point of view, executing the above command only launches a Python built-in Web server. If a file named Index.html is present in the current directory, the contents of the file are displayed by default, and if no such file exists in the current directory, the list of files in the current directory is displayed by default, which is the download server that you see.
FTP server method via Python version
See here, by default you have already installed Python, tested here as python2.7 version
Python does not have a built-in FTP server that can be used directly, so support for third-party components is required, and I find this component called Pyftpdlib, which is installed first:
Pip Install Pyftpdlib
After installation, similar to the HTTP server, execute the following command to start an FTP servers:
Python-m pyftpdlib-p 21

The subsequent 21 ports are still optional, not filled in randomly, and the occupied ports will be skipped. In the browser to address the local computer:
ftp://host Ip:21

Second, the string is converted to JSON
JSON is a lightweight data interchange format that is easy for humans to read and write, but also easy to parse and generate. Because of the many advantages of JSON, it has been widely used in various systems, so we often have the need to convert JSON strings to JSON objects.
For example, in the author's work process, our system will invoke the API of the underlying service, the API of the underlying service is generally returned in JSON format, in order to facilitate problem tracking, we will convert the JSON returned by the API into a string to the log file. When you need to parse a problem, you may need to snap the JSON string from the log file for analysis, and at this point, you need to convert a JSON string to a JSON object for readability.
This requirement is so common that using a search engine to search for "JSON", the first item in search results is the "Online JSON formatting tool". In addition to opening the browser and using the online JSON formatting tool, we can also parse the JSON string using the command line terminal's Python interpreter, as shown here:
$ Echo ' {' job ': ' Developer ', ' name ': ' lmx ', ' sex ': ' Male '} ' | Python-m Json.tool {
"Job": "Developer", "name": "LMX", "Sex": "Male"
}
Parsing the JSON string with the command-line interpreter is very convenient, and for readability, the tool automatically aligns and formats the results of the transformation as follows:
$ Echo ' {"Address": {"province": "Zhejiang", "City": "Hangzhou"}, "name": "LMX", "Sex": "Male"} ' | Python-m Json.tool {
"Address": {
"City": "Hangzhou", "province": "Zhejiang"
}, "name": "LMX", "Sex": "Male"
}
Iii. Check if third-party libraries are installed correctly
Yum Install Python-pip–y #安装python的pip包
Pip Install Paramiko #通过pip安装其他python包 (here pip is equivalent to Yum in Linux)
After installing Python's third-party library, how do you confirm that the library is properly installed? The answer to this question is simple, just try importing imports, and if the import has no errors, the installation is considered successful.
[Email protected]:~/temp$ python
Python 2.7.13 (Default, Feb 10 2017, 20:22:22)
[GCC 4.7.2] on linux2
Type "Help", "copyright", "credits" or "license" for more information.

>> Import Paramiko
Verifying that Python's third-party libraries are installed is a simple matter in itself, but how can we verify that the third-party library installation was successful if we used a script to automate the deployment of a large number of servers? It is certainly not possible to log on to each server for verification, and at this point we can quickly execute the import statement using the parameters of the Python interpreter -c as follows:
Python-c "Import Paramiko"
Using this method of authentication is not only more efficient than interactive authentication, but more importantly, it is possible to implement validation operations on the remote server in the script.
Iv. using Python to extract the ZIP archive package
If the reader is an engineer with relevant work experience, he must have encountered the need to extract the zip-format compression package under Linux. Linux generally uses the tar command to create and read compressed packages, but the tar command does not support zip-format compression packages. In order to read compressed packets in ZIP format, you need to use unzip, and most operating systems do not have unzip installed, so every time you need to extract the zip format of the compression package in Linux will be more troublesome.
In addition to installing the Unzip tool and writing a Python script to extract the zip-format compression package, you can also use the ZipFile module to provide a command-line interface. Readers can later use the Python command to create and decompress compressed zip packages under Linux. The command line interface provided by the ZipFile module contains the following options:
? -L <zipfile> display a list of files in a zip-format zipped package
? -C Create zip-format compression Package
? -e extract zip-format compressed package
? -T validation file is a valid ZIP format compression package
The following command creates, views, and extracts a zip-format compression package using the command-line interface provided by the Python ZipFile module:
Python-m zipfile-c monty.zip spam.txt eggs.txt
Python-m zipfile-e Monty.zip target-dir/
Python-m zipfile-l Monty.zip

Summary: I hope to help everyone, can improve the efficiency of everyone.

Introduction to Python utility widgets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.