[英]Python: check if it socket or file
I want to get data from stdin and pass it over to a server. 我想从stdin获取数据并将其传递到服务器。 And vice versa: get data from the server and display in the terminal.
反之亦然:从服务器获取数据并在终端中显示。 Of course I don't want to block neither the stdin nor the socket.
当然我不想既不阻止stdin也不阻塞。 Here is a piece of code:
这是一段代码:
while True:
inputready = select.select([server_socket, sys.stdin], [], [], self.timeout)[0]
for src in inputready:
...
Is it possible to figure the type of src here? 可以在这里计算src的类型吗?
Are there any other ways, you can think of, to solve the problem? 您能想到还有其他方法来解决问题吗?
Are there any other ways, you can think of, to solve the problem?
您能想到还有其他方法来解决问题吗?
You could just check: 你可以检查一下:
for src in inputready:
if src == server_socket:
...
Now back to your first question, you could: 现在回到您的第一个问题,您可以:
os.fstat
to get a stat buffer os.fstat
获取统计缓冲区 stat.S_ISSOCK
to check if it's a socket stat.S_ISSOCK
检查它是否是套接字 Again, there's little reason to do this. 同样,没有理由这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.