preface
The Python language has become popular in recent years because it allows us to write less code to implement complex functions. The Python developer community welcomes toolkits that encapsulate complex implementations but are user-friendly.
However, Python’s simplicity goes beyond that. Can you believe we can use Python without writing any code? In the next article, I’ll show you eight examples of using Python’s built-in features without writing any code.
0. Python CLI -m parameter
Let’s start with the Python CLI. Although we don’t have to write code to use the functionality described later, we need to use the Python command line to let Python know what we’re about to do.
As long as we have a Python environment installed on our computer, we can type Python –help on the Python command line interface to display all the supported arguments.
Because the command output is too long, the figure above shows only part of it. The most important thing to highlight here is the -m mod argument, which runs Python modules as scripts. Therefore, if the module’s implementation supports command-line operations, we can use it directly from the command line. Let’s try it out 🙂
1. Test service ports
Sometimes, we want to test outbound network traffic on an IP port, and usually the Telnet command is a good choice. The Telnet software is not installed on the Windows platform by default. You need to manually install the Telnet software before using it. If it’s a simple test with few future scenarios, installing it can be a waste of resources.
However, if you have Python installed, you do not need to download and install Telnet because Python has a Telnet module built in. We can test port 443 on a Google search site.
Python -m telnetlib -d 142.250.70.174 443Copy the code
As you can see above, the network traffic is fine and we even received a response from Google null characters. If we try to access a random port of the IP, an error will be thrown, as shown in the figure below.
Python -m telnetlib -d 142.250.70.174 999Copy the code
2. Start the Web service locally
Many Python users don’t know this and are surprised when they first hear about it. Yes, we can start a Web service using Python without writing any code, just by executing the following command from the command line as follows.
python -m http.server
Copy the code
When it runs, it shows that the service is listening on port 8000 locally, and then we can try to access http://localhost:8000/ from the browser.
The Web service presents the local file system at the command startup path as a root directory, in other words, we cannot access its parent directory.
You may ask, what are the usage scenarios for this feature? For example, if you want to share a lot of text /PDF/ image files/subdirectory files etc. in a directory on your computer with your friends, then this method can be very easy to share.
If you want to know more about this topic, see the article 3 Lines of Python Code to Write A Web Server. If you implement a “low code” solution as described in the previous article, you can add more custom functionality to it.
3. Verify and format the JSON string
If you have a very long, unformatted JSON string, it can be very difficult to read. Usually, I use some text editor with JSON plugins, such as Sublime or VS Code, to format JSON strings. However, if you don’t have these tools at hand, Python can be used temporarily. For example, this short JSON string is shown below.
echo '{"name": {"first_name":"Chris", "last_name":"Tao"} "age":33}
Copy the code
As you can see, the command line tool of the current operating system can only display the original string in its original format. However, if you use Python’s json.tool tool, the JSON string is nicely formatted.
echo '{"name": {"first_name":"Chris", "last_name":"Tao"} "age":33}' | python -m json.tool
Copy the code
Oops! The JSON string is invalid, and json.tool helped us locate the problem. We missed a comma after the name object. So add a comma to make the JSON valid.
echo ‘{“name”: {“first_name”:”Chris”, “last_name”:”Tao”}, “age”:33}’ | python -m json.tool
The JSON string now has perfectly indented formatted output! Easier to read.
4. Create a text editor
You read that right, we can use Python to “create” a text editor. Of course, it has very limited features, but it’s a lot easier to use if you don’t have a better option right now. Also, it’s definitely not as functional as Vim and Nanos, but it’s all based on the UI editor rather than command line text. The editor is created by the IDlelib module based on the Tkinter implementation, so it can run cross-platform.
Suppose we were to write a simple Python program to display the current time, and I wanted to write code quickly without downloading and installing a huge code editing tool. Now let’s run the following command.
mkdir get_time_apppython -m idlelib get_time_app/print_time.py
Copy the code
Idlelib cannot be created if the file directory does not exist, so we need to create one if necessary. After we run this command, print_time.py will only be created locally if a save is performed. The editor should now pop up, and we can write some code in there, and you can see that the code supports syntax highlighting.
Now use the CTRL + S shortcut to save the edited code and close the edit window. Then use the command line to look at the edited code file to verify that there are no problems.
cat get_time_app/print_time.py
Copy the code
5. Create an executable application
If we want to create a simple application, such as the one we wrote earlier that gets the current time, we don’t need a third-party toolkit like PyInstaller. Python’s built-in Zipapp can do it. Suppose we want to package a “Get Time” application, we can run the following command from the command line.
python -m zipapp get_time_app -m "print_time:main"
Copy the code
In this command, we simply set the get_time_app name to zipapp and specify the Python program entry file and its program entry function. A file with a.pyz extension is the application we created so that we can distribute projects as individual files instead of folders.
The program is also easy to start with a call from Python.
python get_time_app.pyz
Copy the code
6. Encode and decode strings or files
With the Python CLI, we can encrypt strings or files. Let’s take the interesting ROT13 encryption algorithm as an example. ROT13 is a Caesar cipher with an offset of 13 bits, and its encryption principle is shown in the figure below.
We can use encodings.rot_13 to encrypt a string.
echo "I am Chris" | python -m encodings.rot_13
Copy the code
Remember, don’t use it for any real encrypted content. Since English has 26 letters, running the algorithm again we can easily decipher the encrypted string 🙂
echo 'V nz Puevf' | python -m encodings.rot_13
Copy the code
Now let’s try a more common scenario — Base64 encoding. We can base64 encode strings as shown below.
echo "I am Chris" | python -m base64
Copy the code
Next, we can also decode the encrypted string with the -d argument.
echo "SSBhbSBDaHJpcwo=" | python -m base64 -d
Copy the code
Base64 is also commonly used to encode and decode image files. We can also encode the file as follows.
python -m base64 get_time_app/print_time.py
Copy the code
Interestingly enough, decoded Python scripts can be executed instantaneously without error.
echo "ZnJvbSBkYXRldGltZSBpbXBvcnQgZGF0ZXRpbWUKCgpkZWYgbWFpbigpOgogICAgY3VycmVudF90aW1lID0gZGF0ZXRpbWUubm93KCkKICAgIHByaW50KGY nQ3VycmVudCB0aW1lIGlzIHtjdXJyZW50X3RpbWV9LicpCgoKaWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIG1haW4oKQo=" | python -m base64 -d | pythonCopy the code
7. Obtain system metadata
If we want to get current system information, Python provides a very easy way. All we need to do is run the following command.
python -m sysconfig
Copy the code
As you can see, this command displays all system configuration information, such as the Python environment path and environment variables. The screenshot above only shows part of the content, but the actual content will be quite extensive. If we just want to show the Python environment path and the current working path, we can execute the following command.
python -m site
Copy the code
8. File compression
We can use Python to compress files without downloading tools like tar/zip/gzip. For example, if we wanted to compress the application we just wrote in Section 4, we could run the following command to compress the folder into a ZIP file. In the command, the -c option represents the “create” meaning.
python -m zipfile -c get_time_app.zip get_time_app
Copy the code
Of course, we can also unzip the compressed file. Following the above operation, we unzip the folder and place it in a new directory so that it does not conflict with the original directory. In the command below, the -e option stands for “extract”, which means decompressing.
python -m zipfile -e get_time_app.zip get_time_app_extracted
Copy the code
If you’re not sure, we can check it out.
ls get_time_app_extractedcat get_time_app_extracted/get_time_app/print_time.py
Copy the code
We just demonstrated zip files as an example. Python supports decompression of tar and gzip in addition to zip format.
conclusion
This article describes a way to use Python’s built-in libraries without writing any code. If you can think of using these methods in some scenarios, there is no doubt that it can provide us with a lot of convenience. I hope this article can give you inspiration and help.