python中的轮询套接字问题

时间:2009-09-03 20:31:39

标签: python sockets fork polling

开始轮询循环后,第一次迭代后打印的所有消息都要求我按下终端输入以显示它。

#!/usr/bin/python
import socket, select, os, pty, sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 5007))
s.listen(5)

mypoll = select.poll()
mypoll.register(s.fileno() )

while True:
    print "poll time"
    subr = mypoll.poll()
    for x in subr[0]:

        if x == s.fileno():
            conn, addr = s.accept()

            pid, fd = pty.fork()
            if pid != 0:
                mypoll.register(fd)
                print "done. go back to poll now"
            else:
                print "forked"
                #handles new connection

        else:
            data = os.read(x,1024)
            print data

1 个答案:

答案 0 :(得分:1)

第一次迭代后,你没有注册pty fd,然后轮询它吗?并且它的fd永远不会等于socket fd,所以你将os.read pty fd。现在不是从您的终端读取?因此不输入退货会导致它“打印数据”吗?