Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Socket Programming - need to do something while listening for connections
    text
    copied!<p>I am in the process of designing some hardware interface with python. what I need to do is as follows,</p> <p>~initialize the drivers ~start the device ~create a socket at port 2626 and wait for clients to connect for receiving data ~if any client got connected then send the hello message while serving all other connected client and add this client to the connected client list. ~if any event happened on the device lets say temperature raise is detected then through this event data to all connected clients. ~any connected clients can ask the server for any specific data.</p> <p>This is my process. I got the device part working great now its printing data to console and for the socket server I have this following code this working fine as I expect. but what is problem is after calling "run()" its going inside the while loop. its obvious though. when I am listening for new connections I am not able to call any other function. </p> <p>while listening for connections I should be able to send / recv. any ideas how to do this?</p> <p>this is my server program which is working fine for listening for connections. while listening you are not allowed to to anything. :(</p> <pre><code>#!/usr/bin/env python import socket import select class ChatServer: def __init__( self, port ): self.port = port; self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) self.srvsock.bind( ("", port) ) self.srvsock.listen( 5 ) self.descriptors = [self.srvsock] print 'Server started on port %s' % port def run( self ): while 1: # Await an event on a readable socket descriptor (sread, swrite, sexc) = select.select( self.descriptors, [], [] ) # Iterate through the tagged read descriptors for sock in sread: # Received a connect to the server (listening) socket if sock == self.srvsock: self.accept_new_connection() else: # Received something on a client socket str = sock.recv(100) # Check to see if the peer socket closed if str == '': host,port = sock.getpeername() str = 'Client left %s:%s\r\n' % (host, port) self.broadcast_string( str, sock ) sock.close self.descriptors.remove(sock) else: host,port = sock.getpeername() newstr = '[%s:%s] %s' % (host, port, str) self.broadcast_string( newstr, sock ) def accept_new_connection( self ): newsock, (remhost, remport) = self.srvsock.accept() self.descriptors.append( newsock ) newsock.send("You're connected to the Python server\r\n") str = 'Client joined %s:%s\r\n' % (remhost, remport) self.broadcast_string( str, newsock ) def broadcast_string( self, str, omit_sock ): for sock in self.descriptors: if sock != self.srvsock and sock != omit_sock: sock.send(str) print str, myServer = ChatServer( 2626 ).run() </code></pre> <p>Thanks in advance for all your help :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload