Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to listen to multiple python sockets at once

Tags:

python

sockets

can i listen to multiple sockets at once

The code i am using to monitor the sockets at the moment is:

while True:
    for sock in socks:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print "received message:", data

but that waits at the line:

data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes

until it recieves a message.

Is there a way to make it listen to multiple sockets at once

EDIT: not sure if it is completely relevant but i am using UDP

like image 799
Calum Avatar asked Feb 26 '13 23:02

Calum


People also ask

What is socket Listen 5 Python?

socket. listen(backlog) Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 1; the maximum value is system-dependent (usually 5). Obviously the system value is more than 5 on your system.

What is listen () in Python?

Listen() Function Of Socket Class In Python Calling listen() makes a socket ready for accepting connections. The listen() method should be called before calling the accept() method on the server socket. The listen() function accepts a queue size through the parameter backlog.

Is Python good for socket programming?

Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols.


1 Answers

A slight update to entropy's answer for Python 3: The selectors module allows high-level and efficient I/O multiplexing, built upon the select module primitives. Users are encouraged to use this module instead, unless they want precise control over the OS-level primitives used. As per the documentation

like image 58
PrisionMike Avatar answered Oct 08 '22 12:10

PrisionMike