A Pennsylvania pervert on trial for shooting four Jewish volunteer patrolmen after he was caught masturbating in a rental car in Brooklyn only fired his gun to defend himself from a bloodthirsty mob of Orthodox men, a defense attorney argued today.

A Borough Park mother spotted David Flores, 36, pleasuring himself near her kid and called the neighborhood Shomrim, who chased Flores down in 2010, prosecutors said at trial openings in Brooklyn Supreme Court.

“They surrounded his car and they banged on the windows,” said defense attorney Doug Appel.

“Did he bolt out of the car guns ablazing, or was he trying to save his own life? When the police arrived he was covered in his own blood.”

Prosecutors say Flores — a restaurant supply store worker who was in Brooklyn visiting his mother — armed himself because he knew his lewd actions would make the community furious.

“He knew his actions would cause any community — not just the Orthodox Jewish — to become outraged,” said assistant district attorney Lindsay Ashwal.

“He had something the Shomrim didn’t: a loaded firearm. He was going to do anything he could to avoid being arrested for public lewdness.”

Flores was spotted masturbating near children in the neighborhood twice the week before the shooting and has past arrests for robbery and lewdness, cops said when he was arrested.

He was charged with four counts of attempted murder — plus weapons possession and public lewdness — and could get 100 years on the four top counts.

jsaul@nypost.com