You have it already: writhing in pain is the usual way to express this scenario, and snakes are said to writhe.
Although there is a general transitive sense of writhe meaning to twist something, when referring to the human body, writhing is commonly used to describe the involuntary contortions associated with pain, laughter, anger, or pleasure.
One can also writhe deliberately, as to enjoy a warm bed on a cold morning, or to slink on the dance floor. It suggests more languid movement than wriggling or squirming — snakes typically writhe, worms wriggle, and children squirm. But while it isn't an idiom in the sense of a figure of speech, it is an evocative word. If writhe appears in a news headline, one would not be faulted for immediately assuming someone is either writhing in agony or writhing in ecstasy.
Writhing in pain has become much more popular than writhing with pain, but both can be found in current usage.