ADitya Bimantara ADitya Bimantara - 1 year ago 127
Python Question

poplib error with 3.3 not 2.7: Invalid message number

I am getting an error from code using

on Python 3.3 but which works on Python 2.7:

poplib.error_proto: b"-ERR Invalid message number: b'1'"

I want to migrate to python 3.3 because I have a specific module that is only installed on my python 3.3.

I am learning the python programming language.

Here is the sample that succeeds on python 2.7, but this sample code does not work on my python 3.3.

import poplib

pop_server = ''
user = 'user'
password = 'pass'

p = poplib.POP3(pop_server)

print ("This mailbox has %d messages, totaling %d bytes." % p.stat())

msg_list = p.list()
print (msg_list)

for msg in msg_list[1]:
msg_num, _ = msg.split()
resp = p.retr(msg_num)

Here is the output:

This mailbox has 2 messages, totaling 633300 bytes.
(b'+OK 2 messages:', [b'1 137956', b'2 495344'], 20)

Here is the error traceback:

Traceback (most recent call last):
File "", line 28, in <module>
resp = p.retr(msg_num)
File "C:\Python33\lib\", line 236, in retr
return self._longcmd('RETR %s' % which)
File "C:\Python33\lib\", line 171, in _longcmd
return self._getlongresp()
File "C:\Python33\lib\", line 147, in _getlongresp
resp = self._getresp()
File "C:\Python33\lib\", line 140, in _getresp
raise error_proto(resp)
poplib.error_proto: b"-ERR Invalid message number: b'1'"

Answer Source

You are trying to pass a str as message number. Change the following line

msg_num, _ = msg.split()


msg_num = int(msg.split()[0])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download