Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择未等待指定超时的API_Python_Python 2.7_Pexpect_Socketserver - Fatal编程技术网

Python 选择未等待指定超时的API

Python 选择未等待指定超时的API,python,python-2.7,pexpect,socketserver,Python,Python 2.7,Pexpect,Socketserver,我在同一代码中结合使用Socket编程和pexpect。我已经让它工作,但有一个小故障。select API在第一次迭代中等待指定的5秒。一旦收到来自客户端的输入,它就不再等待5秒,即使它在循环中被指定。简而言之,在第一次客户机-服务器交互发生后,选择无效!我理解如何在C++中绕过这个问题,但是我对Python比较陌生,我无法找出原因。我附上了下面的代码,这是一个非常简单的代码 #!/usr/bin/python #Basic Functionailty: to create a process

我在同一代码中结合使用Socket编程和pexpect。我已经让它工作,但有一个小故障。select API在第一次迭代中等待指定的5秒。一旦收到来自客户端的输入,它就不再等待5秒,即使它在循环中被指定。简而言之,在第一次客户机-服务器交互发生后,选择无效!我理解如何在C++中绕过这个问题,但是我对Python比较陌生,我无法找出原因。我附上了下面的代码,这是一个非常简单的代码

#!/usr/bin/python
#Basic Functionailty: to create a process and to take inputs from client machines
#This input will be given to a background process which will display the user the parsed output
#using pexpect
import pexpect
import socket
import select

TCP_IP = '127.0.0.1'
TCP_PORT = 50050
BUFFER_SIZE = 1024

#spawning a pexpect process and printing everything before the prompt appears
child = pexpect.spawn('./pox.py')
child.expect ('POX>')
print child.before

#binding to a port number and acting as a server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((TCP_IP, TCP_PORT))
server.listen(1)
input = [server]

#loop infinitely and get input and serve it to the process
#in addition to which if the process shows some messages display
#it to the user
while 1:
  print 'Before select'
  inputready,outputready,exceptready = select.select(input,[],[],5)
  for s in inputready:
    if s == server:
      #if the input is from the server
      conn, addr = s.accept()
      print 'Connection address:', addr
      input.append(conn)
      data = conn.recv(BUFFER_SIZE)
      if not data: continue
      print "received data:", data
      child.sendline (data)

    else:
      #if a time out occurs check the pexpect if it has any debug messages
      i = child.expect ([pexpect.TIMEOUT, 'POX>'], timeout=1)
      print child.before
      if i == 0:
        print child.before

您已经修改了
输入

input.append(conn)
(然后调用
conn.recv
以获取一些数据,这些数据将被阻塞,直到有一些数据或EOF,但可能有一些数据,您就得到了它)

这样做之后,在下一次循环中,很可能有接收数据或EOF准备在
conn
上输入。我假设您立即调用
child.expect
(因为
s==conn
,因此
s!=server
)。我敢打赌,在这一点上,
conn
处于EOF,所以它立即返回,什么也没做
conn
仍处于打开状态,并且仍处于
input
状态,因此每次调用
select
时,它会立即返回,告诉您可以从
conn
读取另一个EOF