Running Python modules as scripts
Python has a pretty extensive standard
library. This is great if you need to quickly hack
together a script. There are also a large number of modules which can be run
directly. To run a module directly the -m
option can be used:
-m mod : run library module as a script (terminates option list)
This post is going to go over a few of the more useful modules that can be run directly.
Running a web server
Python ships with a complete web server implementation. This is very helpful if you quickly need to send files across a local network.
$ python -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...
127.0.0.1 - - [01/Nov/2016 22:44:19] "GET / HTTP/1.1" 200 -
You can also specify a port number as an additional argument:
$ python -m http.server 80
Serving HTTP on 0.0.0.0 port 80 ...
127.0.0.1 - - [01/Nov/2016 22:46:42] "GET / HTTP/1.1" 200 -
Note: In earlier versions of Python (2.x), the module is called
SimpleHTTPServer
.
Pretty JSON
RESTful APIs that return JSON are pretty ubiquitous. However it's not uncommon for responses to have no white space:
$ curl -s https://status.github.com/api/messages.json
[{"status":"good","body":"Everything operating normally.","created_on":"2016-11-01T03:44:22Z"},{"status":"minor","body":"We are investigating a high number of exceptions on GitHub.com.","created_on":"2016-11-01T02:55:49Z"}]
The json.tool
module can be used to pretty-print JSON, making it a little
more human readable:
$ curl -s https://status.github.com/api/messages.json | python -m 'json.tool'
[
{
"body": "Everything operating normally.",
"created_on": "2016-11-01T03:44:22Z",
"status": "good"
},
{
"body": "We are investigating a high number of exceptions on GitHub.com.",
"created_on": "2016-11-01T02:55:49Z",
"status": "minor"
}
]
Mimetypes
The mimetypes
modules is pretty self explanatory. It can be used to guess the
mimetype of a file:
$ python -m mimetypes example.jpg
type: image/jpeg encoding: None
$ python -m mimetypes example.tar.gz
type: application/x-tar encoding: gzip
Note: unlike the file command, mimetypes
only looks at the
file extension, not the actual contents of the file.
Archive files
There are also modules which can be used to work with common archive formats
such as tar
and zip
. The tarfile
modules can be used to manipulate tar
files:
$ python -m tarfile -h
usage: tarfile.py [-h] [-v] [-l <tarfile> | -e <tarfile> [<output_dir> ...] |
-c <name> [<file> ...] | -t <tarfile>]
A simple command line interface for tarfile module.
optional arguments:
-h, --help show this help message and exit
-v, --verbose Verbose output
-l <tarfile>, --list <tarfile>
Show listing of a tarfile
-e <tarfile> [<output_dir> ...], --extract <tarfile> [<output_dir> ...]
Extract tarfile into target dir
-c <name> [<file> ...], --create <name> [<file> ...]
Create tarfile from sources
-t <tarfile>, --test <tarfile>
Test if a tarfile is valid
And the zipfile
module can be used to manipulate zip archives:
$ python -m zipfile -h
Usage:
zipfile.py -l zipfile.zip # Show listing of a zipfile
zipfile.py -t zipfile.zip # Test if a zipfile is valid
zipfile.py -e zipfile.zip target # Extract zipfile into target dir
zipfile.py -c zipfile.zip src ... # Create zipfile from sources
Note: the gzip
module can be used to compress/decompress tar files, or
any other file if required.
Telnet client
Although netcat is a far more flexible tool, the telnetlib
module
can be used as a basic telnet client. You could use it to connect to a telnet
server, however it can also be used for other protocols, e.g. HTTP:
$ python -m telnetlib example.com 80
GET / HTTP/1.1
HOST: example.com
HTTP/1.1 200 OK
Cache-Control: max-age=604800
Content-Type: text/html
...
Note: it's worth pointing out that telnet does differ from a raw TCP socket. In addition to sending/receiving raw data, telnet also sends control information. See RFC854 if you're interested in the specifics.
Base64 and ROT13
The base64
modules can be used to, you guessed it, encode and decode
Base64 data. By default data from stdin or a file will be read
and encoded:
$ echo hello world | python -m base64
aGVsbG8gd29ybGQK
$ echo hello world > example.txt
$ python -m base64 example.txt
aGVsbG8gd29ybGQK
However the -d
option can be used to decode data:
$ echo aGVsbG8gd29ybGQK | python -m base64 -d
hello world
There is also a ROT13 module (encodings.rot_13
), which can be used to
transform text using the ROT13 cipher:
$ echo uryyb jbeyq | python -m encodings.rot_13
hello world
Note: unlike base64
, encodings.rot_13
will only read from stdin.
Turtles all the way down
Last, but not least, there is a demonstration of the turtle module:
$ python -m turtle