This post is just a quick post to show, how you can optimize molecules with harmonic constraints in Python with Open Babel. This requires Open Babel to be compiled with Python SWIG-bindings.
There are three types of constraints, distances, angles and torsions. Here is a gist that might help get you started.
Acknowledgements: Kasper Thofte pretty much wrote the above gist back in the days.