how to make a cylinder from p1 to p2

I’m trying to make a cylinder starting from p1 ending on p2. I have written a function but the cylinder are wrong and I can’t understood why.
Here the function :

def cylinder(p1, p2, d1) :
cyl_me = None
sph_me = None
cyl_verts = []

    length = sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2 + (p2.z - p1.z)**2)       # length of the vector
    axe = CrossVecs(p2-p1, z)                                                   # axe for rotation
    if (axe.length != 0) & (axe != z) :
        angle = AngleBetweenVecs(axe, z)
        matRot = RotationMatrix(angle,4,'r',axe)
        cyl_me = Mesh.Primitives.Cylinder(32, d1, length)
        cyl_me.transform(matRot)
        for vert in cyl_me.verts :
            cyl_verts.append(vert.co + p1)
        fOffset = len(all_verts)
        all_verts.extend(cyl_verts)
        for fac in cyl_me.faces :
            face = []
            for v in fac :
                face.append(v.index+fOffset)
            all_faces.append(face)