Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a python script to listen for inputs from another script

I have a situation where I need one python script to be running in a continuous loop, and I need to pass parameters into it from another script which will run when an action occurs.

The second script will be triggered by a website using cgi, I have this working fine. The continuous loop should take in parameters read out by the cgi script (and then send info via a serial port).

For the specific problem I can't have the cgi script sending the data via serial directly as each time it runs it resets the serial port.

I can't seem to find any information on this kind of setup. Are there any methods or libraries I could look into for this or better ways of approaching it?

like image 630
Ger Avatar asked Sep 04 '13 14:09

Ger


People also ask

How do you pass arguments from one script to another in Python?

The simplest way is to just make subprocess1.py accept a single argument on stdin, then simply pass the argument as an addition to the list of arguments in subprocess. call. If you want to actually get anything back you'll need to use subprocess. check_output and parse the output.

How do you auto trigger a Python script?

Double-click on the Task Scheduler, and then choose the option to 'Create Basic Task…' Type a name for your task (you can also type a description if needed), and then press Next. For instance, let's name the task as: Run Hello World. Choose to start the task 'Daily' since we wish to run the Python script daily at 6am.


2 Answers

I would use a socket connection. Essentially you are writing a very simple server that only takes one connection at a time

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", 9988))
s.listen(1)

while True:
    conn, addr = s.accept()
    data = conn.recv(1024)
    conn.close()
    my_function_that_handles_data(data)

s.accept() is a blocking call. It waits for a connection. Then you do a read on the connection. In this case we are assuming the length of the parameters are only 1024 bytes. Then we do something with the data we received from the socket and wait for another connection.

The client could look like this:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 9988))
s.sendall('My parameters that I want to share with the server')
s.close()

The nice thing about this is that in the future if the client and server are no longer running on the same machine then it's a simple matter of changing "localhost" to the actual ip address or domain name you are going to hit.

like image 119
aychedee Avatar answered Oct 13 '22 06:10

aychedee


The general concept of what you're describing is called Inter-Process Communication (Python IPC).

In python this can be done by sockets and signals.

There are also higher level interfaces built on top of/using sockets; asyncore and asynchat.

like image 33
Jo Are By Avatar answered Oct 13 '22 05:10

Jo Are By