Asynchronous sockets with dedicated - Python

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()

This will be the main view of the echo server using select (), but when I run it, I will make error 10038 - attemp to manipulate something that is not a socket. Can someone tell me what is wrong? Thank:)

+5
source share
3 answers

You are working on Windows, right? On Windows, select only works on sockets. But sys.stdin is not a socket. Remove it from line 15 and it should work.

On Linux, etc. I expect it to work as above.

+6
source

I solved a very similar problem before.

, .put Queue.queue, -. , , , -. , running=0.

, -, , , 0,01 . -, Twisted Win32 .

FYI Unix, . , Windows select Winsock. Unix .

, .

EDIT: multiprocessing.pipe

+4

, select -

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

input = [server,sys.stdin] 

sys.stdin ( ).

+2
source

All Articles