Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79771 > unrolled thread
| Started by | Martin <martin@libtec.org> |
|---|---|
| First post | 2014-10-25 15:42 +0300 |
| Last post | 2014-10-27 11:19 +0100 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | Martin <martin@libtec.org> |
|---|---|
| Date | 2014-10-25 15:42 +0300 |
| Subject | asyncio: setting file permissions of a Unix socket? |
| Message-ID | <mailman.15229.1414399500.18130.python-list@python.org> |
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()
[toc] | [next] | [standalone]
| From | Michael Ströder <michael@stroeder.com> |
|---|---|
| Date | 2014-10-27 11:19 +0100 |
| Message-ID | <m2l679$q72$1@dont-email.me> |
| In reply to | #79771 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web