I used this function for intersection of finite line segments and it worked perfectly:
http://paulbourke.net/geometry/pointlineplane/L3D.py