Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79771
| Date | 2014-10-25 15:42 +0300 |
|---|---|
| From | Martin <martin@libtec.org> |
| Subject | asyncio: setting file permissions of a Unix socket? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15229.1414399500.18130.python-list@python.org> (permalink) |
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
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