Anthony Weiner — the former New York congressman whose career and marriage were wrecked by sexting scandals — sobbed Monday as a federal judge sentenced him to nearly two years in prison for X-rated communication with a 15-year-old girl.

It was the denouement of a three-act drama that took Weiner, 53, from the hallowed halls of the U.S. Capitol to a wood-paneled courtroom in the city he once hoped to lead as mayor.

"I was a very sick man for a long time," Weiner told the judge in asking for leniency. "I have a disease but I have no excuse."

Former Congressman Anthony Weiner arrives at U.S. Federal Court on Monday for sentencing after pleading guilty to one count of sending obscene messages to a minor. Lucas Jackson / Reuters

The disgraced Democrat's voice began cracking moments after he started reading a statement in which he invoked his soon-to-be ex-wife, Huma Abedin, and their young son. Abedin, a former aide to Hillary Clinton, was not in the courtroom, but Weiner's parents and brother sat in the first row.

Related: Why Anthony Weiner's Sentence Could Have Been Worse

Pausing at times to wipe his eyes with a tissue, Weiner said that even after his predilections were publicly exposed twice before, forcing him to resign from Congress and dooming a mayoral run, he remained in denial.

"I convinced myself my behavior wasn't really a problem," Weiner, 53, said.

But now, he said, he realizes he is "an addict," attends therapy twice a week and is trying to help others with sexual compulsions. "I ask you for the opportunity to prove that it's real," he told U.S. District Judge Denise Cote. "If I fail, I know what will happen, but with God's grace I know I will not."

A courtroom sketch shows Anthony Weiner during his sentencing. Christine Cornell

Weiner regained his composure as he waited for the judge to hand down the sentence, but when she said he would have to serve 21 months, he began crying again.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

His head bowed, he held his hands over his face as she spelled out the remaining terms of the sentence, including supervised release for three years, DNA collection and federal and state sex offender registration. Weiner is prohibited from having contact with the victim and must take part in a sex offender treatment program, Cote ruled. He was also ordered to pay a $10,000 fine, but no restitution.

He will remain free until Nov. 6. It has not been decided where he will do his time. His lawyer said that being sent to the federal jail in Manhattan, a maximum security facility, would make it difficult for Weiner to see his son and asked that he be sent to a lower-security facility. But the judge said the most she could do was not recommend the lockup closest to New York City.

The charge to which Weiner pleaded guilty, one count of transferring obscene material to a minor, carries a maximum of 10 years in prison.

Prosecutors asked for 21 to 27 months, telling the court that on three separate occasions Weiner went online from his Manhattan apartment "and asked a real 15-year-old girl to display her naked body for him and to sexually perform."

"There is a history here that simply cannot be ignored," Assistant U.S. Attorney Amanda Kramer said, referring the earlier scandals. "What required to stop the defendant from re-offending is a meaningful term of imprisonment."

Weiner's lawyer, Arlo Devlin-Brown, pushed for probation, saying he would not get the treatment he needs in federal prison. He focused on what Weiner had not done: he didn't initiate the contact with the teen, he didn't try to meet her and he didn't collect child pornography or display the hallmarks of pedophilia.

"This was his first teenage and his last teenage victim," Devlin-Brown said.

Cote's remarks suggested she was trying to strike a balance between the seriousness of the offense and Weiner's efforts at rehabilitation.

She said she had received letters from the 15-year-old's father and grandfather, but did not disclose their contents. The motives of the girl, who sold her story to a British newspaper, played no role in her decision, she said. Instead, the judge said, the opportunity to send a message that would deter others from preying on minors was a significant factor.

"This was a serious crime and it's a serious crime that deserves a serious punishment," Cote said. "She was a minor. She was a victim. She's entitled to the law's full protection."

The sentencing capped a six-year scandal that revealed Weiner to be a savvy, ambitious politician by day and a reckless Internet lothario by night.

It began unfolding in 2011 when he accidentally tweeted a photo of his bulging crotch, prompting his resignation from Congress amid revelations the misfire wasn't a one-time transgression.

Anthony Weiner, a former Democratic congressman, leaves Federal Court in New York on Monday after being sentenced for 21-months for sexting with a 15-year-old girl. Timothy A. Clary / AFP - Getty Images

Two years later, Weiner mounted a run for mayor of New York, but that campaign was derailed by revelations that he had continued to have steamy conversations online, using the alias Carlos Danger, after he quit the House to focus on being a "better husband" to a pregnant Abedin.

When the last round of allegations surfaced in 2016 — including reports that Weiner had transmitted a photo of himself shirtless and aroused, with his son asleep on the bed next to him, to a mother of two — Abedin announced she was leaving him. Their divorce is not yet final.

The probe became a factor in the presidential election when James Comey, the FBI director at the time, announced that an investigation into Clinton's private server needed to be reopened because of emails found on a laptop used by Weiner and Abedin. Clinton has said the development contributed to her loss to Donald Trump.

In a letter before sentencing, Abedin asked the judge for leniency on behalf of the couple's son, describing Weiner as a good father.

"With Anthony, I have repeatedly found myself in circumstances I never imagined," she wrote. "I am devastated by Anthony's actions, and I understand he must face their consequences."

Following the sentencing, Weiner's lawyers issued a statement saying they were "disappointed" that he would be imprisoned.

"Judge Cote reasoned that because of Anthony's notoriety, a sentence of imprisonment could discourage others from following in his footsteps. We certainly hope this public service message is received, but it has resulted in a punishment more severe than it had to be given the unusual facts and circumstances of this case," they said.