Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79771

asyncio: setting file permissions of a Unix socket?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <martin@libtec.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'finally:': 0.07; 'made.': 0.07; 'python3': 0.07; 'socket': 0.07; 'subject:file': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'currently,': 0.09; 'data):': 0.09; 'main()': 0.09; 'permissions': 0.09; 'subject:setting': 0.09; 'try:': 0.09; 'def': 0.12; 'suggest': 0.14; 'data)': 0.16; 'from:addr:martin': 0.16; 'main():': 0.16; 'permissions.': 0.16; 'where?': 0.16; 'later': 0.20; 'starts': 0.20; 'code,': 0.22; 'example': 0.22; 'import': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; "shouldn't": 0.24; 'skip:e 30': 0.24; 'skip:l 30': 0.24; 'server.': 0.24; 'file.': 0.24; "i've": 0.25; 'permission': 0.26; 'code:': 0.26; 'skip:" 20': 0.27; 'unix': 0.29; "i'm": 0.30; 'code': 0.31; 'fixing': 0.31; 'way?': 0.31; 'workaround': 0.31; 'file': 0.32; 'class': 0.32; 'interface': 0.32; 'skip:_ 10': 0.34; 'maybe': 0.34; 'problem': 0.35; "can't": 0.35; 'connection': 0.35; 'created': 0.35; 'except': 0.35; 'done.': 0.35; 'but': 0.35; 'revert': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'skip:o 20': 0.38; 'server': 0.38; 'thank': 0.38; 'to:addr:python- list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'users': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'content-disposition:inline': 0.62; 'happen': 0.63; 'received:79': 0.64; 'default': 0.69; 'wish': 0.70; 'connection.': 0.74; 'listening': 0.74; 'from:name:martin': 0.84
Date Sat, 25 Oct 2014 15:42:12 +0300
From Martin <martin@libtec.org>
To python-list@python.org
Subject asyncio: setting file permissions of a Unix socket?
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
User-Agent Mutt/1.5.21 (2010-09-15)
X-Mailman-Approved-At Mon, 27 Oct 2014 09:45:00 +0100
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15229.1414399500.18130.python-list@python.org> (permalink)
Lines 66
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414399500 news.xs4all.nl 2891 [2001:888:2000:d::a6]:48292
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79771

Show key headers only | View raw


Hi! :)

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?

The problem is that the socket file is created when the programs
starts listening for the connections.  Because of this, I can't change
the file permission before a connection is made.  At the same time, a
connection can't happen because of the file permissions.

Currently, I workaround this with os.umask(0o000).  But I also want to
make other files later with default permissions.  So I have to revert
the umask, but where?  I can only do this in the client connection
code, which is out of place.

Bellow is what I've done.  Can you suggest a better way?  I wish to
avoid permission fixing code like os.umask() for every connection.
Maybe I shouldn't use asyncio.Protocol in the first place?

Thank you! :)

Example code:

#! /usr/bin/env python3

import os
import asyncio

class ExampleServer(asyncio.Protocol):
    def __init__(self):
        os.umask(0o002)    # This is my workaround.  Can I avoid this?
        # if not umask_restored: # An alternative workaround.
        #     os.umask(0o002)

    def connection_made(self, transport):
        self.transport = transport

    def data_received(self, data):
        print("Data: ", data)
        self.transport.write(b"OK, bye!\n")
        self.transport.close()


def main():
    socket_filepath = "/tmp/example-server.socket"
    loop = asyncio.get_event_loop()
    server_coroutine = loop.create_unix_server(ExampleServer, socket_filepath)
    server = loop.run_until_complete(server_coroutine)

    os.umask(0o000)             # This is my workaround.

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        print("exit")
    finally:
        server.close()
        loop.close()
        os.remove(socket_filepath)


if __name__ == "__main__":
    main()

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

asyncio: setting file permissions of a Unix socket? Martin <martin@libtec.org> - 2014-10-25 15:42 +0300
  Re: asyncio: setting file permissions of a Unix socket? Michael Ströder <michael@stroeder.com> - 2014-10-27 11:19 +0100

csiph-web