You are not logged in.

#1 2023-06-01 21:29:04

ELODollZ
Member
Registered: 2023-02-06
Posts: 33

[Discarded]Getting an array list from bash, passed into python script

Hey I'm back again big_smile
Again i'm pretty close to a deadline for a school project,
i need my bash code to read the array that comes in, and assign it to a variable, then pass that variable to my BashInput variable in python code.
hope you can help i got deadline in 18 hours, sorry the tight deadline
My code get's the output
# can't copy it, because it is from a RPI, but i have the app.py runing on a serbert "nohup" so it's not sopposed to run the whole code, i just want it to pass to my variable

[LIST OF DATA COMES IN, AND GET PRINTED FINE]
Flask enviroment app.py
problemes, emit to app.py fails

# Python3 Code

#!/usr/bin/env python3

# Imports
import sys

# Variables
message = ""

# Main Code
def GetInputFromBash():
    BashInput = (sys.argv[])
    message = str(BashInput)

# Bash Code

#!/bin/bash

# Variables
Fullmessages=""
message=""
SentMessages="Messages sent"
IFS=','

# Main Code
while true
do
mosquitto_sub	-h	localhost	-t	"1Year/ESP32Data" -C	1	-R	$Fullmessages | while read Name0 Value0 Name1 Value1 Name2 Value2 Name3 Value3 Name4 Value4 Fixed;
do
	echo $Name0 $Value0 $Name1 $Value1 $Name2 $Value2 $Name3 $Value3 $Name4 $Value4 $Fixed
	messages = $Name0 $Value0 $Name1 $Value1 $Name2 $Value2 $Name3 $Value3 $Name4 $Value4 $Fixed
	echo $messages
	python app.py $messages
done
done

Last edited by ELODollZ (2023-06-02 13:33:22)

Offline

#2 2023-06-01 22:05:12

papajoke
Member
From: france
Registered: 2014-10-10
Posts: 40

Re: [Discarded]Getting an array list from bash, passed into python script

and where is the error ? bash .. python ???? you want what ?

python : message = " ".join(sys.argv[1:])

bash : why not use : python app.py "$messages" ?

Last edited by papajoke (2023-06-01 22:07:35)


lts - zsh - Kde - Intel Core i3 - 6Go RAM - GeForce 405 video-nouveau

Offline

#3 2023-06-02 01:11:04

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 646

Re: [Discarded]Getting an array list from bash, passed into python script

papajoke wrote:

bash : why not use : python app.py "$messages" ?

That's still ugly as heck. Just pipe the mosquitto subscription output directly into the python program:

mosquitto_sub	<whatever> | python app.py

where app.py is:

import sys
for message in sys.stdin:
    print(message)

Offline

#4 2023-06-02 07:45:19

ELODollZ
Member
Registered: 2023-02-06
Posts: 33

Re: [Discarded]Getting an array list from bash, passed into python script

papajoke wrote:

and where is the error ? bash .. python ???? you want what ?

python : message = " ".join(sys.argv[1:])

bash : why not use : python app.py "$messages" ?

The problem is that it tries to run my app.py webserver, and it failes on the flask_socketio, because it is allready runining.
I just need the Bash script to insert the variable "messages" into the "BashInput" Variable in python. without trying to run, the the flask application itself.
it keeps saying

modulenotfounderror: flaskSocketIO is not found

which ofc it isn't since it is allready runining, i just want it to assign the variable into the script not try to run the whole code.
i have even tried to move the pass into a seperat script, and just call the variable from there, but it keep's trying to run the application from flask

Last edited by ELODollZ (2023-06-02 07:47:26)

Offline

#5 2023-06-02 08:22:22

ELODollZ
Member
Registered: 2023-02-06
Posts: 33

Re: [Discarded]Getting an array list from bash, passed into python script

found a scuffed way to get the output, with a ssh
like, STOP TRYING TO RUN FLASK, i'm getting really anoyoned

["['DeviceNumber", 'ESP32 #1', ' Temp1', ' 78', ' Temp2', ' 23', ' Temp3', ' 40', ' Status', ' True', 'SomeTime', " Fixed']"]
Traceback (most recent call last):
  File "/home/Gruppe9PI/1.YearProject/bilag/Scripts/app.py", line 5, in <module>
    from flask_socketio import SocketIO, emit
ModuleNotFoundError: No module named 'flask_socketio'

Bash Code

# Variables
messages=""
IFS=','

# Main Code
while true
do
mosquitto_sub   -h      localhost       -t      "1Year/ESP32Data" -C    1       -R      $messages | while read messages
do
echo "$messages"
python BashInput.py "$messages"
done
done

Python app.py

# Import libary list:
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import time
import threading
from BashInput import GetInputFromBash, messages

# Variables:
sensor = 25
messages = None
app = Flask(__name__)
socketio = SocketIO(app)

# App route paths:
@socketio.on('hentTemp')
def HentTemperatur():
    message = GetInputFromBash(messages)
    time.sleep(0.5)
    socketio.emit('HentTemperatur', message)
@app.route('/')
def index():
    return render_template('Index.html')


# Main Code:
def readTemp():
    global message
    while True:
        time.sleep(2)
        try:
            #message = sensor.read()
            #message = sensor
            HentTemperatur()
            #print(message)
        except:
            message = None

tempThread = threading.Thread(target=readTemp)
tempThread.start()

# Host WebPage:
if __name__ == '__main__':
    socketio.run(app, host="0.0.0.0", debug=True)

BashInput.py

# Imports
import sys

# Variables
messages = ""
BashInput = ""
# Main Code
def GetInputFromBash(BashInput):
    # BashInput.clear()
    for BashInput in sys.stdin:
        messages += [BashInput]
    BashInput = messages
    print(BashInput)
    return BashInput

Last edited by ELODollZ (2023-06-02 08:46:50)

Offline

#6 2023-06-02 08:42:24

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,154

Re: [Discarded]Getting an array list from bash, passed into python script

You need to provide more context here.  If you have a server application you need to send it the data in some way, are you sure the requirement is to randomly run a mosquito_sub in bash and pass that to python via arguments instead of e.g. some kind of request to the server application? There are MQTT clients for python where you can directly subscribe to a topic and get the information directly from within python e.g. https://archlinux.org/packages/extra/an … paho-mqtt/ ... FWIW we also once had a ruling that discouraged homework questions but I'm unable to find that so I'll let it slide for now

Also if you are hard set on mosquitto_sub and bash, what you'll likely want to do is run your bash script/the mosquitto_sub command as a subprocess within the python process where you can then directly read the stdout: https://docs.python.org/3/library/subprocess.html

Last edited by V1del (2023-06-02 08:45:43)

Offline

#7 2023-06-02 08:51:00

ELODollZ
Member
Registered: 2023-02-06
Posts: 33

Re: [Discarded]Getting an array list from bash, passed into python script

V1del wrote:

You need to provide more context here.  If you have a server application you need to send it the data in some way, are you sure the requirement is to randomly run a mosquito_sub in bash and pass that to python via arguments instead of e.g. some kind of request to the server application? There are MQTT clients for python where you can directly subscribe to a topic and get the information directly from within python e.g. https://archlinux.org/packages/extra/an … paho-mqtt/ ... FWIW we also once had a ruling that discouraged homework questions but I'm unable to find that so I'll let it slide for now

Also if you are hard set on mosquitto_sub and bash, what you'll likely want to do is run your bash script/the mosquitto_sub command as a subprocess within the python process where you can then directly read the stdout: https://docs.python.org/3/library/subprocess.html

Yes, because it is an "upgrade" from our first test, the HTML works and takes the information from certain variables and what not from the app.py, and get's the information from the TempMeasure code. And i have confirmed that the data get's sent just fine from the ESP32 to the RPI 3A+ it is being printed just fine in the shell.
The list of data is complet nothing missing.

The problem is that, i can't get the "FinalSubscriber.bash" to pass the information to the python code "BashInput.py" into the function that takes the suggested "sys.argv[1]" i have confirmed in every way, that the data can get sent to the app.py fine from the Bashinput.py, but it just won't take the argument from my bash code "FinalSubscriber.py"

Offline

#8 2023-06-02 09:13:24

ELODollZ
Member
Registered: 2023-02-06
Posts: 33

Re: [Discarded]Getting an array list from bash, passed into python script

An idea just struck me, is it because Bash passes the code to a "Nonerunining" script, and the just removes the values, so it is empty when i want it to be read?
(also the subprocess seems like a good thing, but never used it and not even sure where to begin to set it up)

Offline

#9 2023-06-02 10:01:05

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,154

Re: [Discarded]Getting an array list from bash, passed into python script

Pretty much, the problem is that by running a python script with variables from bash you are executing an entirely new program that you won't be able to read information from on the server side as is. If your goal is to get values into the server application either configure some kind of end point (e..g instead of running the command again via bash, you set up an end point in your flask that you send the data to in a POST request via curl or so so you can do

curl -d "Name0=${Name0}&Value0=${Value0} ..." -X POST "http://localhost:8080/getSensorData"

so you can then parse the POST parameters, or you write the values you receive into a temporary file that you read on the server on request, or...

This depends on what your exact assignment (and I'd assume the method by which you should be getting the data into the server will be defined/given as it appears to be an entry level course) is and this is the kind of question you'd rather ask your peers or your teacher since they will have the relevant context as to how you should go about this.

Last edited by V1del (2023-06-02 10:06:56)

Offline

#10 2023-06-02 12:01:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,374
Website

Re: [Discarded]Getting an array list from bash, passed into python script

ELODollZ, what class(es) are you taking that have this as homework?  Based on your forum activity, it seems you are being assigned tasks that are far beyond your skill level.  This is not productive for learning: you'll progress much faster if you spend some time actually learning some fundamentals of process I/O and IPC before you work on the kinds of tasks you've been given as homework.

You need to walk before you can run.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2023-06-02 12:51:49

ELODollZ
Member
Registered: 2023-02-06
Posts: 33

Re: [Discarded]Getting an array list from bash, passed into python script

Trilby wrote:

ELODollZ, what class(es) are you taking that have this as homework?  Based on your forum activity, it seems you are being assigned tasks that are far beyond your skill level.  This is not productive for learning: you'll progress much faster if you spend some time actually learning some fundamentals of process I/O and IPC before you work on the kinds of tasks you've been given as homework.

You need to walk before you can run.

i learn the best by swining on the deep end of the pool big_smile
Jokes aside, it allmost works (my only problem now is that it, sends the wrong message) ['run'] is what get through and not my list of information.
For context, we are trying to automate a district heating, so the consumers don't have to adjust the thermostat all the time.

Current code, that's semi working (just sending the wrong message)
Python3 code that get's information from Bash

# Imports
import sys

# Variables
BashInput = ""

# Main Code
def GetInputFromBash():
    var1 = sys.argv[1]
    print(var1)
    BashInput = var1.split(";")
    print(BashInput)
    return BashInput
#BashInput = GetInputFromBash()
#print(BashInput)

Bash Code:

# Variables
messages=""

# Main Code
while true
do
mosquitto_sub   -h      localhost       -t      "1Year/ESP32Data" -C    1       -R      $messages | while read messages
do      
        messages=$(printf "%s;" "$messages")
        echo $messages
        python3 BashInput.py "$messages"
done
done

Offline

#12 2023-06-02 13:20:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,374
Website

Re: [Discarded]Getting an array list from bash, passed into python script

ELODollZ wrote:

it allmost works

The goal of education is not for you to get this one project to work, but for you to understand how it works.  Flailing randomly until it produces the results you want - or copy/pasting someone else's solution so it produces the results you want may allow you to finish your assigment but will contribute nothing to your education.

We have rules here about helping with homework / assignments.  While it is allowed, the spirit of the rule is that we should not be robbing a student of their learning opportunities.  And in your case, the help you need is beyond your capacity to understand: so if we give you answers at all related to the problem you are presenting, we are violating the spirit of the homework rules for the forum.

I'd happily help you work through samples from the wooledge.org bash wiki, ryanstutorials.com, or other similar tutorials suitable to where you are in your learning.  But helping you with this project combining bash, python, flask, and mosquitosub, in a client-server setting is silly - doing so may allow you to get a passing grade, but it will hinder your learning.  I will not do so, and I'd encourage other community members to also avoid doing so.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#13 2023-06-02 13:33:01

ELODollZ
Member
Registered: 2023-02-06
Posts: 33

Re: [Discarded]Getting an array list from bash, passed into python script

Trilby wrote:
ELODollZ wrote:

it allmost works

The goal of education is not for you to get this one project to work, but for you to understand how it works.  Flailing randomly until it produces the results you want - or copy/pasting someone else's solution so it produces the results you want may allow you to finish your assigment but will contribute nothing to your education.

We have rules here about helping with homework / assignments.  While it is allowed, the spirit of the rule is that we should not be robbing a student of their learning opportunities.  And in your case, the help you need is beyond your capacity to understand: so if we give you answers at all related to the problem you are presenting, we are violating the spirit of the homework rules for the forum.

I'd happily help you work through samples from the wooledge.org bash wiki, ryanstutorials.com, or other similar tutorials suitable to where you are in your learning.  But helping you with this project combining bash, python, flask, and mosquitosub, in a client-server setting is silly - doing so may allow you to get a passing grade, but it will hinder your learning.  I will not do so, and I'd encourage other community members to also avoid doing so.

Fair enough, i just learn better that way.
take for example the sys.argv[], i didn't even know that the sys, would allow to pass an augment from outside, i only knew it allowed to control when to stop the code, or control the system (deep sleep an what not)

Offline

#14 2023-06-02 21:03:41

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,692

Re: [Discarded]Getting an array list from bash, passed into python script

Trilby wrote:

[Flailing randomly until it produces the results you want

Sounds like some of those with whom I have worked -- they seemed to have a dependency on the SQA group wink

We have rules here about helping with homework / assignments.  While it is allowed, the spirit of the rule is that we should not be robbing a student of their learning opportunities.  And in your case, the help you need is beyond your capacity to understand: so if we give you answers at all related to the problem you are presenting, we are violating the spirit of the homework rules for the forum.

This is absolute.  When it is a homework assignment, we will guide you and provide feedback, but all the code needs to be that which you write.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB