Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous sockets with select - 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()

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

like image 577
user1505497 Avatar asked Aug 30 '12 10:08

user1505497


People also ask

What does select () do in Python?

Python's select() function is a direct interface to the underlying operating system implementation. It monitors sockets, open files, and pipes (anything with a fileno() method that returns a valid file descriptor) until they become readable or writable, or a communication error occurs.

What is the difference between synchronous sockets and asynchronous sockets?

The send, receive, and reply operations may be synchronous or asynchronous. A synchronous operation blocks a process till the operation completes. An asynchronous operation is non-blocking and only initiates the operation. The caller could discover completion by some other mechanism discussed later.

Is socket listen blocking Python?

All socket methods are blocking. For example, when it reads from a socket or writes to it the program can't do anything else. One possible solution is to delegate working with clients to separate threads.

Can a socket send and receive at the same time Python?

You can send and receive on the same socket at the same time (via multiple threads). But the send and receive may not actually occur simultaneously, since one operation may block the other from starting until it's done.


1 Answers

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.

like image 125
wollud1969 Avatar answered Oct 01 '22 05:10

wollud1969