How to pass information to a Python daemon?

I have a Python daemon running on a Linux system. I would like to convey information such as โ€œBob,โ€ โ€œAlice,โ€ etc., and have the demon seal โ€œHello Bob.โ€ and "Hello Alice" to the file.

This should be asynchronous. The Python daemon must wait for information and print it whenever it receives something.

What would be the best way to achieve this?

I was thinking of a named channel or Queue library, but there could be better solutions.

+6
source share
8 answers

Here's how you can do it with fifo:

 # receiver.py import os import sys import atexit # Set up the FIFO thefifo = 'comms.fifo' os.mkfifo(thefifo) # Make sure to clean up after ourselves def cleanup(): os.remove(thefifo) atexit.register(cleanup) # Go into reading loop while True: with open(thefifo, 'r') as fifo: for line in fifo: print "Hello", line.strip() 

You can use it like this from a shell session

 $ python receiver.py & $ echo "Alice" >> comms.fifo Hello Alice $ echo "Bob" >> comms.fifo Hello Bob 
+6
source

There are several options.

1) If the daemon should accept messages from other systems, make daemon an RPC server - use xmlrpc / jsonrpc.

2) If everything is local, you can use either TCP sockets or Named PIPE.

3) If a huge set of clients is connected at the same time, you can use select.epoll.

+3
source

There are several mechanisms you could use, but it all comes down to using IPC (interprocess communication).

Now, the actual mechanism that you will use depends on the details of what you can achieve, a good solution, although it would use something like zmq.

See the following example in pub / sub on zmq

http://learning-0mq-with-pyzmq.readthedocs.org/en/latest/pyzmq/patterns/pubsub.html

also this

http://learning-0mq-with-pyzmq.readthedocs.org/en/latest/pyzmq/multisocket/zmqpoller.html

for a non-blocking method.

0
source

python has a built-in rpc library (using xml to encode data). The documentation is well written; there is a complete example:

(python 2.7) or

(python 3.3)

what can be considered.

0
source

I am not very good at python, so I would like to share it ** Universal inter-process communication **

nc aka netcat is a client server model that allows you to send data such as text and files over the network.

nc benefits

  • Very easy to use
  • IPC even between different programming languages
  • Built into most Linux OS

Example
On deamon

 nc -l 1234 > output.txt 

From another program or shell / terminal / script

 echo HELLO | nc 127.0.0.1 1234 

nc can be python using the system command invocation function (maybe os.system) and read stdout.

0
source

Why not use signals?

I am not a python programmer, but presumably you can register a signal handler in your daemon and then pass it from the terminal. Just use SIGUSR or SIGHUP or similar.

This is the usual method that you use to rotate log files or similar files.

0
source

One solution might be to use an asynchronous library , which simplifies calls between the server and the client.

Here is an example that you could use (adapted from this site )

In deamon.py , a ChatServer object is ChatServer . Each time a connection is made, a ChatHandler object is inherited from asynchat.async_chat . This object collects data and fills it in self.buffer .

When calling a special line calls the terminator, the data must be completed and the found_terminator method is found_terminator . It is in this method that you write your own code.

In sender.py you create a ChatClient object inherited from asynchat.async_chat , establish a connection in the constructor, define a terminator (in case of a server response!), And call the push method to send data. You must add a terminator string to your data so that the server knows when it can stop reading data.

daemon.py:

 import asynchat import asyncore import socket # Terminator string can be changed here TERMINATOR = '\n' class ChatHandler(asynchat.async_chat): def __init__(self, sock): asynchat.async_chat.__init__(self, sock=sock) self.set_terminator(TERMINATOR) self.buffer = [] def collect_incoming_data(self, data): self.buffer.append(data) def found_terminator(self): msg = ''.join(self.buffer) # Change here what the daemon is supposed to do when a message is retrieved print 'Hello', msg self.buffer = [] class ChatServer(asyncore.dispatcher): def __init__(self, host, port): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.bind((host, port)) self.listen(5) def handle_accept(self): pair = self.accept() if pair is not None: sock, addr = pair print 'Incoming connection from %s' % repr(addr) handler = ChatHandler(sock) server = ChatServer('localhost', 5050) print 'Serving on localhost:5050' asyncore.loop() 

sender.py:

 import asynchat import asyncore import socket import threading # Terminator string can be changed here TERMINATOR = '\n' class ChatClient(asynchat.async_chat): def __init__(self, host, port): asynchat.async_chat.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect((host, port)) self.set_terminator(TERMINATOR) self.buffer = [] def collect_incoming_data(self, data): pass def found_terminator(self): pass client = ChatClient('localhost', 5050) # Data sent from here client.push("Bob" + TERMINATOR) client.push("Alice" + TERMINATOR) 
0
source

All the FIFOs mentioned (called channels in Linux terminology) and XML-RPC, but if you are learning these things right now, you also need to check TCP / UDP / Unix sockets, as they are platform independent (to the least extent, sockets TCP / UDP). You can check out this tutorial for a working example or Python Documentation if you want to go in that direction. This is also useful since most modern communication platforms (XML-RPC, SOAP, REST) โ€‹โ€‹use these basic things.

0
source

Source: https://habr.com/ru/post/983971/


All Articles