python#
run raw Python code directly on the command line#
python -c "print('This is a message from Python!')"
print resolved Python executable’s filepath#
python -c "import sys; print(sys.executable)"
print resolved Python executable’s version#
python -c "import sys; print(sys.version)"
print resolved Python executable’s import path#
python -c "import sys; print('\n'.join(sys.path))"
print Python environment configuration#
python -m site
pretty-print JSON#
python -m json.tool file.json
validate JSON#
cat file.json | python -m json.tool > /dev/null
base64 encode a string#
echo "secret" | python -m base64
base64 decode a string#
echo "c2VjcmV0Cg==" | python -m base64 -d
create and administer a sqlite3 database#
python -m sqlite3 my.db
test DNS resolution#
python -c "import socket; print(socket.gethostbyname('example.com'))"
run a TCP server on port 9999 that serves 127.0.0.1#
python -c "import socket as s; sock=s.socket(); sock.bind(('127.0.0.1',9999)); sock.listen(1); print(sock.accept())"
run a TCP server on port 9999 that serves external traffic#
python -c "import socket as s; sock=s.socket(); sock.bind(('0.0.0.0',9999)); sock.listen(1); print(sock.accept())"
run an HTTP server on port 8000 that serves 127.0.0.1#
python -m http.server 8000
run an HTTP server on port 8000 that serves external traffic#
python -m http.server 8000 --bind 0.0.0.0
run an HTTP server on port 8000 that serves a specific directory#
python -m http.server 8000 --directory /tmp