asyncore回调的问题

asyncore回调的问题

模仿例子写了一个测试程序,发现连接服务器成功后没有回调handle_connect,为什么?

--------------------------------
class itsocket(asyncore.dispatcher):
        def __init__(self, host, port):
                asyncore.dispatcher.__init__(self)
                self.host = host
                self.port = port
                self.status = 0
                self.rbuffer = ''
                self.sbuffer = ''
                self.callDict = {0x0a00 : self.onVerify, \
                                                 0x1000 : self.onInitMarket, \
                                                 0x0100 : self.onTrace, \
                                                 0x0400 : self.onReal, \
                                                 0x0c00 : self.onTextInfo}
                self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
                self.connect((host, port))

        def handle_connect(self):
                print ''.join(['已连接', self.host, ':', str(self.port)])

---------------------------------
class itquote_trd(threading.Thread):
        def __init__(self, threadname, sock):
                threading.Thread.__init__(self, name = threadname)

        def run(self):
                print '异步线程启动'
                asyncore.loop()
                print '异步线程停止'

----------------------------------
sock = itsocket.itsocket(ihost, iport)
用法不对,loop()是全局的,不要放在线程中。
http://www.chmhome.com/Python_St ... l-chp-7-sect-4.html
在if __name__ == '__main__':下面加上这句话,还是没有回调handle_connect
上面给出链接是<<python standard library>>对asyncore的举例,简化了一下

[Copy to clipboard] [ - ]
CODE:
import asyncore
import socket

class Request(asyncore.dispatcher):

    def __init__(self, host, port=80):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))

    def writable(self):
        return 0 # don't have anything to write

    def handle_connect(self):
        print "connected"

    def handle_expt(self):
        pass

    def handle_read(self):
        self.close()
        pass

    def handle_close(self):
        self.close()

request = Request("www.163.com", 80)
asyncore.loop()

4楼的例子运行以后屏幕上没有任何显示,是不是要装第三方包?我是windows环境
囧掉了,楼上用的什么版本,我的python 2.5.1,正常。
一样的251