Anthony Weiner learned his fate ... he's going to federal lockup for just under 2 years.

The ex U.S. Congressman was sentenced in Manhattan for transferring obscene material to a minor. Prosecutors had recommended a 27-month sentence, and the judge could have given him as much as 10 years ... so the 21 months might be the best possible outcome for Weiner.

However, the judge admonished him repeatedly during sentencing. He said Weiner "has a disease that involves sexual compulsivity; some call it a sex addiction." Weiner wept openly in court during the hearing.

Weiner pled guilty in May to sexting with a 15-year-old girl in North Carolina. In court today, the judge said Weiner still has a chance to contribute to society if he continues getting treatment for his illness.

He will have to register as a sex offender once he gets out, and he'll be under supervised released. He'll also be under a computer Internet monitoring program.

Weiner has to surrender by Nov. 6 to enter prison.

WALK OF SHAME
TMZ.com