Python3.10.4 serial readline() not displaying prompts - python-3.x

Right now, I am connecting to a device using Tera Term and manually running over 50 tests. My goal is to automate these tests. Currently I am trying to do this through python using the pyserial library. The issue I am running into is readline() will display everything until the login prompt appears. It stops on the line before.
What it should look like
Booting: 2022.08
CPU: cpu_id
In: serial
OUT: serial
Normal Boot
Hit any key to stop autoboot: 0
Starting Kernel ...
Enter your credentials
cpu_id login:
what readline() is displaying:
Booting: 2022.08
CPU: cpu_id
In: serial
OUT: serial
Normal Boot
Hit any key to stop autoboot: 0
Starting Kernel ...
Enter your credentials
Here is the code I am currently playing with:
Any insight would help. Or, if you know of another way to run tests through a serial port, I'm all ears.Thanks!

Thanks to the comments of #ShadowRanger I was able find a work around. I ended the readline() loop the line before the login prompt. I then logged in and ran a test blind then started reading again afterwards. Here is the code I used:
This is not the cleanest code but it explains the process well enough.

Related

Linux Capabilities: CAP_SYS_NICE=ep results in AttachNotSupportedException on JDK11+

Currently running on a fedora machine with JDK 11.0.15, and java capabilities set to "cap_sys_nice+ep".
The command that I am testing is "jcmd JFR.start ....". However, I end up getting the error,
AttachNotSupportedException. Unable to open socket file tmp/attach_pid1234: "
"target process 1234 doesn't respond within 10000 ms"
Process 1234 was launched via JDK11.0.15 java with the CAP_SYS_NICE capability set.
However, when I attempt to do a Java Flight Recording, I always get the error above. jcmd does not have any capabilities set.
What really puzzles me is the fact that, given the same process run using JDK11.0.15, if I utilize jcmd from JDK8 or JDK7, everything works fine.
The only solution I have found is by giving JDK11.0.15 jcmd CAP_SYS_PTRACE=ep capabilities. Could anyone explain what exactly is going on in the background? Thanks.

Profiling in Odoo

I am new to odoo and code profiling. I am using py-spy to profile my odoo code, as I need a flame graph as the output of profiling. Everything is working fine with py-spy, but the issue is, the py-spy needs to be stopped by pressing ctrl + C on the terminal where it is running or shutting the odoo server down. I can't stop or reset the odoo server neither I can do ** Ctrl + C** on the server.
I had tried to create to do this
To start py-spy
def start_pyflame(self):
pyflame_started = self.return_py_spy_pid('py-spy')
error = False
if not pyflame_started:
self.start_pyflame()
else:
error = 'PyFlame Graph process already created. Use Stop button if needed.'
_logger.error(error)
which is working fine, the problem is with this one
def stop_pyflame_and_download_graph(self):
pyflame_running = self.return_py_spy_pid('py-spy')
if pyflame_running:
subprocess.run(["sudo", "pkill", "py-spy"])
Now the issue is when I am killing the process with pkill or kill it kills the process but along with this, it also terminates the py-spy, because of which, the output file is not generated.
Is there any way to stop or soft kill py-spy so that the output file will be created.
Thanks in advance for help
After some research, I came to know that all these kill commands are just killing the process whereas in this case, we need to stop the process.
This thing I have achieved by
sudo kill -SIGINT <pid>
As it is clear from the name, this command is not killing/terminating the process, it is simply asking the process to stop working by passing an interrupt signal.
This worked for me.

How to pass "yes/no" to prompt from python code?

I have tiny little python code which logs in to a remote machine and gets the output of a command and prints to a file..
Here for a particular command, the server asks for Y or N, how do i pass an Yes to it and get the desired output?
Here is the sample output from the server:
root#nnodee11cc40c:/home/usr/redsuren# controlport rst 0:3:3
WARNING: Port 0:3:3 may have active Smart SAN target driven zones that may be disrupted. If changing the port configuration, remove the Smart SAN zoning by using the removehost command. This must be done before changing the port configuration; otherwise, you will not be able to manage the zone on the switch associated with this port.
Are you sure you want to run controlport rst on port 0:3:3?
select q=quit y=yes n=no: ---------> Here i have to tell the program to enter y
How can I achieve this?
Thanks!!
If I understand your question correctly, you need to automate pressing 'y' & 'enter' with Python. You can easily do this by PyAutoGui.
First, execute pip install pyautogui in the command prompt.
Then import it to your code by using import pyautogui
Now for achieving this you have to put the following code where you want to press 'y' & 'enter':
pyautogui.press('y')
pyautogui.press('enter')
But this may not be timed according to when it asks so you may need to time it yourself
by adding time.sleep(<numberOfSeconds>) after importing it by import time
Now here is the full answer:
import pyautogui, time
# Your code here
time.sleep(3)
pyautogui.press('y')
pyautogui.press('enter')
But if my answer is not what you asked for, then you have to give us your code so we can understand your question better thus answer it to your needs.

How to get past command prompt hang without manually hitting enter? [Python - Selenium]

I am trying to webscrape with python via selenium, and for one of the websites, after scraping the information this error pops up:
[11796:3768:0425/173530.772:ERROR:navigation_entry_screenshot_manager.cc(135)] Invalid entry with unique id: 35
A similar error happens when I try to log in to a website(https://www.waug.com/good/?idx=104880) via selenium. My code is:
import selenium.webdriver
option = selenium.webdriver.ChromeOptions()
option.add_argument(' - incognito')
browser = selenium.webdriver.Chrome(executable_path=r"chromedriver.exe")
browser.get('https://www.waug.com/good/?idx=104880')
browser.find_element_by_name('btn-login').click()
browser.find_element_by_id('mem_id').send_keys(myID)
browser.find_element_by_id('mem_pwd').send_keys(myPW)
browser.find_element_by_css_selector("button[type='submit']").click()
Then I get the error: [11364:1060:0425/175320.588:ERROR:navigation_entry_screenshot_manager.cc(135)] Invalid entry with unique id: 5
Now the real problem is that when I get these errors, the program doesn't break but my command prompt just hangs until I manually hit enter. Is there a way to make my program hit enter on its own to get past this error? I'm scraping a bunch of similar websites and I don't want the program to wait for me to hit enter manually before continuing on. For now I don't care too much about why the error is popping up(although it'd be great to know if anyone does know), so maybe is there a way to suppress the error over all?

Python - pySerials inWaiting() always return 0

I'm trying to make a small program that receives messages from the serial port, and doing it periodically. Right now I'm not saving anything, I'm just trying to see if I get anything at all, so I tried this code:
def ReceiveRS():
global ser
while ser.inWaiting() > 0:
print(ser.read(1))
ser is the serial port, which is correctly initialized, as it has worked before and I can send stuff. After trying some different things I have found out that inWaiting() never seems to return anything but 0. Anyone have any ideas as to why and how to fix it?
Oh, and I'm using Python 3.2.3, with pySerial on a Raspberry PI.
This was embarrassing. I had another older version running in the background (on autostart so I didn't remember it was running) that was taking all the received bytes and leaving none for the newer script. So, anyone know how to remove questions?
try this
while (True):
if ser.inWaiting() > 0:
print(ser.read(1))
should be working now as expected.

Resources