I want to get all the messages from my gmail inbox, but I am facing 2 problems.

It does not get all the emails, (as per the count in stat function)
The order of emails it get is random.

I am unsure if its the problem with poplib or gmail pop server.
What am I missing here?
You can also try imaplib module since GMail also provides access to email via IMAP protocol.
Poplib not working correctly?