4
\$\begingroup\$

I'm trying to implement simple data streamer in Python with sockets. Consider one server and one or more clients. I just want to read data from server, process it and send it to the client. Here's how I do it now:

        outputsocket = socket.socket()
        outputsocket.bind((self.outputaddress, self.outputport))
        outputsocket.listen(2)
        inputsocket = socket.socket()
        rsocks, wsocks = [], []
        rsocks.append(outputsocket)
        rsocks.append(inputsocket)
        recv_data = []

        try:
            inputsocket.connect((self.inputaddress, self.inputport))
            while True:
                try:
                    reads, writes, errs = select.select(rsocks, [], [])
                except:
                    return
                for sock in reads:
                    if sock == outputsocket:
                        client, address = sock.accept()
                        wsocks.append(client)
                    elif sock == inputsocket:
                        data_from_socket = sock.recv(8192)

                if data_from_socket:
                    outdata = process_data(data_from_socket)
                    if wsocks:
                       reads, writes, errs = select.select([], wsocks, [], 0)
                       for sock in writes:
                           sock.send(outdata)
        except KeyboardInterrupt:
            inputsocket.close()
            outputsocket.close()
            # etc

Obviously, it's stripped down example, but I think you got the idea. Does anyone have better ideas?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You might want to put inputsocket.close() and outputsocket.close() in a finally: block, instead of in the exception block, as a general clean up measure. \$\endgroup\$
    – voithos
    Dec 22, 2011 at 19:34

1 Answer 1

2
\$\begingroup\$

For the benefit of others (because this answer is two years late):

I had to implement a socket solution rapidly to accommodate testing a feature that required them. Ended up using socketserver to simplify the implementation, i.e., only about a dozen lines were needed.

Example below is pretty much straight out of the socket server docs:

""" creates, activates a socket server """
import socketserver

HOST, PORT = "localhost", 9999


class MyTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())


print("Creating the server, binding to localhost on port", PORT)
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)


def activate_server():
    server.serve_forever()


def stop_server():
    print("Shutting the server on port", PORT)
    server.shutdown()


if __name__ == "__main__":
    activate_server()
\$\endgroup\$

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.