Anthony Weiner, weeping in court Friday, agreed to serve more than 2 years in prison and pleaded guilty to sexting with a 15-year-old girl, a scandal that reverberated in last year’s presidential campaign.

Under the terms of a plea bargain, the 52-year-old former Democratic congressman — whose career and comeback attempts were felled by revelations that he couldn’t stop exchanging explicit messages and photos with young women — must register as a sex offender anywhere he lives, studies or works.

“I destroyed my life’s dream of public service,” Weiner, choking up, told U.S. District Judge Loretta Preska in Manhattan.

The plea calls for a federal prison sentence of between 21 and 27 months, though the sentencing judge is free to impose more or less time.

The crime to which he pleaded guilty — transferring obscenity to a person younger than 16 — carries a maximum 10 years.

“I have a sickness, but I do not have an excuse,” he said, dabbing his eyes with a tissue. “I apologize to everyone I have hurt. I apologize to the teenage girl, who I mistreated so badly.”

advertisement | advertise on newsday

Preska allowed Weiner, who lives in Manhattan, to remain free before sentencing, on Sept. 8. He must give up his cellphone — the one he used to send the teen the explicit images.

“You have agreed to forfeit, essentially, an iPhone,” the judge said, before accepting the plea.

The FBI opened a criminal probe of Weiner in September, after a 15-year-old girl from North Carolina told a tabloid that the two traded prurient messages and he’d asked her to strip on camera.

In court Friday, Weiner said, “I knew this was as morally wrong as it was unlawful.”

“I was contacted by, and began exchanging online messages with, a stranger who said that she was a high school student and who I understood to be 15 years old,” he said, adding: “I engaged in obscene communications with this teenager, including sharing explicit images and encouraging her to engage in sexually explicit conduct, just as I had done and continued to do with adult women.”

The FBI’s search of his laptop uncovered emails between Hillary Clinton, the Democrats’ nominee for president, and Weiner’s wife, Huma Abedin, one of Clinton’s closest aides. Abedin — who The Associated Press reported filed for divorce on Friday — was not in the courtroom for her husband’s plea.

The email discovery prompted the FBI to reopen a criminal probe in October into the security and legality of a private email server the Clintons set up in her Westchester basement instead of using the State Department’s system when she was secretary of state. The probe was re-closed the following month without any charge. Clinton has blamed her loss, in part, on then-FBI Director James Comey for publicizing the laptop discovery.

Until the 2011 revelation that he had a habit of trading lewd tweets with adoring strangers, Weiner was a promising star in the Democratic Party, a telegenic congressman whose brash liberalism made him a frequent TV talking head and front-running mayoral candidate.

advertisement | advertise on newsday

Weiner, under pressure from his party, resigned from Congress in 2011 after the first sexting scandal, then made a failed run for mayor in 2013 that collapsed after he was revealed to have continued to sext under a pseudonym: Carlos Danger.

“I remained in denial even as the world around me fell apart,” he told the judge Friday. “This fall, I came to grips for the first time with the depths of my sickness.”

With William Murphy