Would it be possible to hook a command prior to save a file?

My use case: I want to "compile" or "parse" my source file to check syntax errors prior to save a python file since once it's saved, I have a web server that reloads itsel when the sources change but crashes if it finds a syntax error.