python select,套接字读取缓冲区获取数据,但选择不返回

时间:2018-08-03 07:58:10

标签: python select

我遇到一个非常奇怪的问题。 我使用linux(centos 6.5)/ python(2.7)选择来实现网络服务器以与多个网络设备进行通信。 问题是,运行几次后,需要5分钟到几个小时, 即使我知道套接字读取缓冲区应该获取新数据,但选择不返回,只需挂断即可。代码如下:

ip="x.x.x.x"
port1 = 8322
server = socket.socket()
server.setblocking(False)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server.bind((ip,port1));
server.listen(10)
cli_socket = socket.socket()
cli_socket.setblocking(False)
host = "127.0.0.1"
port2 = 12355
cli_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
cli_socket.bind((host, port2))
cli_socket.listen(5)

inputs = [服务器,cli_socket,]

while True:
readable, writable, exceptional = select.select(inputs, outputs, inputs, 30)
    for s in readable:
        if s is server:
            conn,address = server.accept()
            rec_buf=conn.recv(1024)
            //make some handle here, store this connection in global data
            conn.setblocking(0)
            conn.sendall("pass")
            inputs.append(conn)
        elif s is cli_socket:
            cli_conn, addr = s.accept()
            command = cli_conn.recv(1024)
            ret=command.split(',')
                if ret[0] == "OPEN":                                                
                    total_key[ret[1]].conn_sock.sendall("open")
                    cli_conn.send("success")
                    cli_conn.close()
        else:
            rec_buf=s.recv(1024)                                        
            //make some handle here                        
            s.sendall("ok")
    for s in exceptional:
        inputs.remove(s)
        s.close()

0 个答案:

没有答案