hakk blog

Python One liners

A couple of helpful(?) one liners in python, I use these for quick development or when troubleshooting and testing.

Start a static file http server in the current directory

python -m http.server

If you only want it to listen on localhost you can pass a bind parameter

python -m http.server --bind 127.0.0.1

It’s also possible to specify a port (default is 8000)

python -m http.server --bind 127.0.0.1 8080

If you need an smtp server for trouble shooting, it’s possible to fire one of those up in one line. It will just print all output to the terminal but most of the time that works just fine for my purposes. You my need elevated privileges to run on port 25 or any other privileged port.

python -m smtpd -n -c DebuggingServer localhost:25

Pretty print JSON right in the command line, it’s not strictly Python but when combined with curl it’s quite nice.

curl -X GET "https://httpbin.org/json" -H "accept: application/json" | python3 -m json.tool

Know of other helpful python one liners? Let me know, please.