2
votes

I am programming a simple IMAP client for gmail. As it is an assignment to use sockets and so on, i cannot use python's imaplib library. I encountered a problem within this code:

 def connect(self, server, username, password, port=993):

    self.socket.connect((server, port))
    response = self.socket.recv(2048)
    if response.startswith('NO' or 'BAD'):
        return False
    print response

    # send username
    self.sendData('a001 login {0} {1}\n'.format(username, password))
    response = self.socket.recv(2048)
    if response.startswith('NO' or 'BAD'):
        return False
    print response 

    self.sendData('a002 CAPABILITY\n')
    response = self.socket.recv(2048)
    if response.startswith('NO' or 'BAD'):
             print response
             return False
    print response 
    return True

Everything works fine, until the second command to be sent. I'm not really sure, what i am in now.

Here is sendData which was asked:

  def sendData(self, command):

    lenght = len(command)
    totalSendBytes = 0

    while (totalSendBytes < lenght):
        bytesSend = self.socket.send(command[totalSendBytes:])

        if bytesSend <= 0:
            return False

        totalSendBytes += bytesSend

    return True

After launching the program, i get response only to first command :

  • OK Gimap ready for requests from IPGOESHERE 47if12049394eef.11

  • CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE a001 OK [email protected] XXX XXX authenticated (Success)

Thank you in advance.

1
Can you post the code for sendData? Also your indentation is off under the CAPABILITY if statement. Can you post the error/stacktrace you are getting? - John
There is no error of indentation, it is check by compiler. There are no errors. - Mantas Marcinkus

1 Answers

1
votes

Try using \r\n, which is required by the IMAP specification.