开发者

Calling recv hangs after handle is called

开发者 https://www.devze.com 2023-02-19 21:51 出处:网络
I\'ve got a Python class I\'m using to emulate an IMAP server for a unit test. When the handle() callback happens, I try to use recv() to get the data that just arrived but I\'m blocking on the recv()

I've got a Python class I'm using to emulate an IMAP server for a unit test. When the handle() callback happens, I try to use recv() to get the data that just arrived but I'm blocking on the recv() call. I thought recv() would return with the data that was received but instead, it just sits there.

In case it matters, I can see that the server and main thread are different in the log output.

I'm assuming that this has something to do with the fact that the socket was just opened but no data was sent through it. Do I need to check to see if that's the case before trying to call recv()?

I've created the server using:

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass
...
# inside another class, we've got:
def startUp(self):
    server = ThreadedTCPServer(('localhost', 5593), FakeImapServerHandler)
    server.allow_reuse_address = True
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.setDaemon(True)
    server_thread.start()

Then inside some other code in o开发者_开发百科ur unit test, we try to connect to the fake server:

    print("about to connect")
    self.__imapCon = imaplib.IMAP4("localhost", 5593)
    print("about to login") # we never get here
    self.__imapCon.login(Config.config["imapUser"], Config.config["imapPassw"])

The server is implemented like:

class FakeImapServerHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        print("handling request")
        incoming = self.request.recv(1024)
        print("request was: " + incoming ) # never get here


(Posted as an answer this time) Per the IMAP protocol, servers speak first, sending an OK greeting to the client. Both your server and client seem to be trying to recv() from each other at the same time:

atlas% telnet chaos 143
Trying 192.168.1.5...
Connected to chaos
Escape character is '^]'.
* OK chaos Cyrus IMAP4 v2.2.13-Debian-2.2.13-19 server ready
0

精彩评论

暂无评论...
验证码 换一张
取 消