Three underutilized python commands

Today’s post are three simple Python commands which should be known more widely, but are not.

Start a simple web server to serve files from current directory.

~$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

This is neither secure, nor scalable, but handy if you are in a directory and want to quickly test something.

Testing emails

~$ python -m smtpd -n -c DebuggingServer

Starts a fake SMTP server on port 1025. Useful if you are testing emails on a system which doesn’t allow pluggable emails backends.

Pretty print JSON

echo '{"2legs":"Better", "4Legs": "Good", "6Legs": "Spider"}' | python -m json.tool
  • Inspired by a tweet from Adrian Holovaty
  • The -m switch was added in PEP-338
  • Try these too python -m unittest discover ., python -m timeit -s 'import random; 10*random.randint(10, 20)'

Thank you for reading the Agiliq blog. This article was written by shabda on May 1, 2014 in Python .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here