asyncio: setting file permissions of a Unix socket?

Michael Ströder michael at stroeder.com
Mon Oct 27 06:19:26 EDT 2014


Martin wrote:
> I'm using the asyncio.Protocol interface to build a server which binds
> to a unix socket file.  I want other system users to connect to the
> unix socket, so to communicate with the server.
> 
> Where should I set the permissions of the file?

You should start the demon with a strict umask and set the permissions after
the socket is created.

Deriving from SocketServer.UnixStreamServer I'm overriding the server_bind()
method:

class MyServer(SocketServer.UnixStreamServer):
[..]
  def server_bind(self):
    """Override server_bind to set socket options."""
    self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    self.socket.settimeout(SOCKET_TIMEOUT)
    try:
      os.unlink(self.server_address)
    except OSError:
      if os.path.exists(self.server_address):
        raise
    SocketServer.UnixStreamServer.server_bind(self)
    os.chmod(self.server_address,int(SOCKET_PERMISSIONS,8))
    return # server_bind()

Ciao, Michael.




More information about the Python-list mailing list