That's a good question, because after you have the baby they will prescribe 6 weeks of "vaginal rest," which includes sex *and* masturbation because they want the area to rest entirely after a baby goes through it.
But I am sure the "no sex" instruction here is to prevent you from getting new microorganisms in your body from your partner's body. If your hands are perfectly clean it is likely you will not give yourself an infection. You can call your doctor who prescribed the meds and ask the nurse, to be sure.
I would see no harm in masturbation. So, 31 weeks . . . when is your baby due?