5

i am studying asynchronous sockets right now and i have this code:

#!/usr/bin/env python 

""" 
An echo server that uses select to handle multiple clients at a time. 
Entering any line of input at the terminal will exit the server. 
""" 

import select 
import socket 
import sys 

host = 'localhost' 
port = 900 
backlog = 5 
size = 1024 
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
server.bind((host,port)) 
server.listen(backlog) 
input = [server,sys.stdin] 
running = 1 
while running: 
    inputready,outputready,exceptready = select.select(input,[],[]) 

    for s in inputready: 

        if s == server: 
            # handle the server socket 
            client, address = server.accept() 
            input.append(client) 

        elif s == sys.stdin: 
            # handle standard input 
            junk = sys.stdin.readline() 
            running = 0 

        else: 
            # handle all other sockets 
            data = s.recv(size) 
            if data: 
                s.send(data) 
            else: 
                s.close() 
                input.remove(s) 
server.close()

It shoud be a basic kind of echo server using select(), but when I run it, I ger select error 10038 - attemp to manipulation with something which is not socket. Can someone tell me what is wrong? Thank you:)

3 Answers 3

6

You are working on Windows, aren't you? On Windows select works only on sockets. But sys.stdin is no socket. Remove it from line 15 and it should work.

On Linux or the like I would expect it working as listed above.

3
  • Thanks for answer, yeah I am wroking on windows. So what can i do to make system input working on windows like in this example? Commented Aug 31, 2012 at 9:44
  • 2
    Well, to be honest, I'm not sure. What I could think of is the following: handle sockets using select in one thread and handle files (like sys.stdin) each in a separated thread using blocking IO. This is certainly not the very best solution, but it should work, especially as you only use sys.stdin to terminate the whole think.
    – wollud1969
    Commented Aug 31, 2012 at 10:09
  • 1
    Here's a code snippet showing how you can work around the select-on-stdin problem under Windows. This code will work under both Windows and POSIX-y OS's: public.msli.com/lcs/jaf/windows_stdin_select_example.py Commented Jun 24, 2013 at 4:05
4

I've solved a very similar problem before.

What you can do is run the asynchronous loop in its own thread, and have the main thread .put data into a Queue.queue when the user types something in. Each time around the loop you can check to see if the queue has anything in it. If it does you act on it, in your case by setting running=0.

If you do this, you need to give select a timeout that's short enough to be responsive to user input, say 0.01 seconds. Don't be disgusted by this use of a timeout, it's how Twisted's Win32 select reactor deals with this issue.

Just FYI this sort of thing is a million times easier on Unix systems where you can select on more than just sockets. I believe that on Windows the select system call is provided by Winsock. On Unix you can select on anything with a file descriptor.

Let me know if you need more clarification.

EDIT: You can also take a look at multiprocessing.pipe

2

Regarding to documentation, the right way to interact with select is

ready_to_read, ready_to_write, in_error = select.select(potential_readers,
                                                        potential_writers,
                                                        potential_errs,
                                                        timeout)

In you code,

input = [server,sys.stdin] 

sys.stdin is not a socket (file descriptor instead).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.