Threading TCP server as proxy between connected user and unix socket

I am writing a web application where I need to move data from server to connected clients. This data can be sent from any other script from the web application. For example, one user makes some changes on the server, and other users need to be notified about it.

So my idea is to use a unix socket (path to the socket based on user #ID) to send data to the corresponding user (web application scripts will connect to this socket and write data). The second part is ThreadingTCPServer, which will accept user connections and send data from unix socket to user over TCP socket connection.

Here's the workflow:

  • Used to connect to a TCP server
  • Django script open unixsocket and write data to it.
  • TCP Server reads data from a unix socket and sends it to open a connection to the user.

Hope you can understand my idea :)

So I have 2 questions:

1. What do you think of my idea as a whole? Is this a good or bad decision? Any guidance is appreciated.

2. Here is my code.

import SocketServer
import socket
import netstring
import sys, os, os.path
import string
import time

class MyRequestHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        try:
            print "Connected:", self.client_address

            user = self.request.recv(1024).strip().split(":")[1]
            user = int(user)
            self.request.send('Welcome #%s' % user)

            self.usocket_path = '/tmp/u%s.sock' % user
            if os.path.exists(self.usocket_path):
                os.remove(self.usocket_path)

            self.usocket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
            self.usocket.bind(self.usocket_path)
            self.usocket.listen(1)

            while 1:
                usocket_conn, addr = self.usocket.accept()
                while 1:
                    data = usocket_conn.recv(1024)
                    if not data: break
                    self.request.send(data)
                    break
                usocket_conn.close()
                time.sleep(0.1)

        except KeyboardInterrupt:
            self.request.send('close')
            self.request.close()

myServer = SocketServer.ThreadingTCPServer(('', 8081), MyRequestHandler)
myServer.serve_forever()

      

and i got an exception

  File "server1.py", line 23, in handle
    self.usocket.listen(1)
  File "<string>", line 1, in listen
error: (102, 'Operation not supported on socket')

      

0


a source to share


3 answers


I guess you shouldn't be using UNIX sockets. If your application (someday) becomes popular or mission critical, you cannot simply add another server to add scalability or make it redundant and fault-tolerant.

If, on the other hand, you put the data in fe memcached (and the "user dataset" number as a separate key). You will be able to put data into memcached from multiple servers and read it from multiple servers. If the user disconnects and connects to another server, you can still retrieve data for him.



You can also use a database (to make it more fault-tolerant) or a combination of database and memcached if you like, but I've seen an application using unix sockets as you are trying and the programmer regretted it later. The table could have userid, data and timestamp columns and you could remember the last timestamp for a given user.

+1


a source


It's too complicated. Use the socketserver module please.



0


a source


You must use Twisted, or more specifically Orbited , to transfer data to your clients. The sample code you posted has a number of potential problems, and it would be much more difficult to figure out what they are than just using a pre-built piece of code to do what you want.

0


a source







All Articles