Apr-26-2021, 03:01 PM
Hello ,
as I understand using pexpect will help me run a "program" inside python
for example run bluetoothctl in pi and run command insdie this interface.
so I want to start from start and understadn the command
this is what I did :
what did I do wrong?
where do I tell him that if nothing for 20 seconds - print "error in bluetooth" ad close exit the code or something ?
*** I have many more question , but let start from this :-)
as I understand using pexpect will help me run a "program" inside python
for example run bluetoothctl in pi and run command insdie this interface.
so I want to start from start and understadn the command
this is what I did :
import pexpect
def Enterbluetooth():
child = pexpect.spawn('bluetoothctl') ## the command I want to run - enter bluetoothctl interface
pexpect.TIMEOUT(20)
print (child.read()) ## print the output of the command as is
child.expect(pexpect.EOF)
if __name__ == '__main__':
Enterbluetooth()but I'm getting this error after ~30 seconds , why?python3 pexpect_example.py
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 111, in expect_loop
incoming = spawn.read_nonblocking(spawn.maxread, timeout)
File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 482, in read_nonbloc king
raise TIMEOUT('Timeout exceeded.')
pexpect.exceptions.TIMEOUT: Timeout exceeded.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pexpect_example.py", line 10, in <module>
Checkbluetooth()
File "pexpect_example.py", line 5, in Checkbluetooth
print (child.read()) ## print the output of the command as is
File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 441, in read
self.expect(self.delimiter)
File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 341, in expect
timeout, searchwindowsize, async_)
File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 369, in expect_list
return exp.expect_loop(timeout)
File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 119, in expect_loop
return self.timeout(e)
File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 82, in timeout
raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0xb66d9e50>
command: /usr/bin/bluetoothctl
args: ['/usr/bin/bluetoothctl']
buffer (last 100 chars): b'Agent registered\r\n\x1b[0;94m[bluetooth]\x1b[0m# '
before (last 100 chars): b'Agent registered\r\n\x1b[0;94m[bluetooth]\x1b[0m# '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 24722
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
0: EOFI gave 20 seconds timeout , so why I got error \ except and got out the code?what did I do wrong?
where do I tell him that if nothing for 20 seconds - print "error in bluetooth" ad close exit the code or something ?
*** I have many more question , but let start from this :-)
