插座和管道的select.select问题 [英] select.select issue for sockets and pipes

查看:96
本文介绍了插座和管道的select.select问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个同时使用管道和套接字的基本python脚本.管道当前保存来自html表单的传入数据,套接字建立了到服务器的连接,该服务器以各种时间间隔发送TCP/IP命令.表单和服务器位于同一LAN上,但位于不同的计算机上.

I am currently in the process of writing a basic python script that makes use of both pipes and sockets. The pipe currently holds incoming data from an html form, the socket makes up a connection to a server that sends TCP/IP commands at various intervals. The form and server are on the same LAN but different computers.

我的代码如下:

#!/usr/bin/env python
import os,sys,time
from socket import *

HOST = 'xxx.xxx.xxx.xxx'
PORT = 6000
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
count = 0
pipe_name = 'testpipe'

if not os.path.exists(pipe_name):
    os.mkfifo(pipe_name)

def readpipe():
    pipein = open(pipe_name, 'r')
    line = pipein.readline()[:-1]
    print line
    pipein.close()

def readTCP():
    data = tcpCliSock.recv(BUFSIZ)
    print data

while count == 0:
    readTCP()
    readpipe()

我意识到count变量目前是多余的,我一直在使用它来尝试调试一些较早的问题

如您所见,代码非常简单明了,并且只要数据首先进入管道然后再进入套接字并保持此模式,代码就可以正常工作.问题在于它不能动态地同时运行这两个功能.它在等待任一插座管道输入时挂起.我试图找到一种方法来识别管道或套接字上是否没有数据,然后它可以消失并检查其他数据.到目前为止,我尝试过的所有操作都没有效果,例如检查空字符串.

As you can see the code is pretty straightforward and works as long as data comes in first on the pipe then the socket and keeps this pattern. The problem is that it is not dynamically running through both functions. It hangs while waiting for input at either the pipe of socket. I am trying to find a way to identify if no data is present on either the pipe or socket, then it can go off and check the other. Everything I have tried so far has had no effect such as checking for an empty string.

我在以下文章中发现了一些有趣的东西,但似乎与我正在做的事情(使用stdin)有很大不同,所以我不确定这是否是我真正想要的东西,或者是否可能改编;

I have found some interesting stuff on the following post but it seems quite a bit different to what I am doing (using stdin) so I'm not sure if it is what I'm really looking for or whether it could be adapted;

访问根据用户"的建议,我现在整理了一些附加代码:

Following on from the advice by 'User' I have now put together a bit of additional code:

while True:
   inputdata,outputdata,exceptions = select.select([tcpCliSock],[],[])
   if tcpCliSock in inputdata:
      data = tcpCliSock.recv(BUFSIZ)
      print data

尽管此方法有效,但我仍无法发现要在管道的输入数据中放入什么标识符.我已经阅读了select模块,显然它确实适用于管道,但是关于它的信息似乎很少,它全都是联网的东西. 我尝试了以下操作(取自上面的主要代码):

although this works I have not been able to discover what identifier to put into inputdata for the pipe. I have read up on the select module and apparently it does work with pipes but there seems to be very little info on it, it's all networked stuff. I have tried the following (taken from main code above):

 testpipe
 pipe_name
 pipein

但是我遇到各种错误,其中大多数与 NameError-未定义 TypeError相关:参数必须为int或具有fileno()方法

But i get various errors most of which are related to a NameError - not defined or TypeError: argument must be an int, or have a fileno() method

推荐答案

通常,您将为此使用select模块.

Usually you would use the select module for this.

import select
sockets_with_read_data, sockets_with_write_data, sockets_with_some_data = \
    select.select([sock, pipe], [], [], timeout = None)

但是我不知道这是否适用于管道.

But I do not know wether this works with pipes.

这篇关于插座和管道的select.select问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆