Misha Slyusarev
Misha Slyusarev

Reputation: 1383

Python: check if it socket or file

I want to get data from stdin and pass it over to a server. 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. 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?

Are there any other ways, you can think of, to solve the problem?

Upvotes: 2

Views: 1914

Answers (1)

cnicutar
cnicutar

Reputation: 182754

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:

Again, there's little reason to do this.

Upvotes: 7

Related Questions