I am writing a code to automate testing using Pexpect So basically Telnet into a system and send commands. I have spawned a child for it. And I pass the child to function. Is it pass by reference or pass by value?
def test(child,clicommand,min,max,message):
child.sendline("xyz")
"""Basically I am sending only sendline But I need 2 more expects.
Because of two execute called before this method.
Both the expects are not blocking and the execute"""
child.expect("() >")
child.expect("() >")
child.expect("() >")
return
def execute(child,clicommand):
print "Executing CLI command: "+clicommand
child.sendline(clicommand)
child.expect("() >")
return
child = pexpect.spawn("telnet xx.xx.xx.xx xxxx")
child.sendline("")
child.expect("() >")
execute(child,"abc")
execute(child,"abc")
test(child,"xyz",1,2,"Error")
So are the expects being buffered or lined up? How Can I make it asynchronous? I even tried try except, but no exceptions are thrown
As stated here , you can't do asynchronous input/output with the same process, so a locking algorithm like this might work for you:
from threading import Lock
lock=Lock()
def send_command(str, int): #int is the repetition number
with lock:
child.sendline(str)
for x in range(int):
child.expect("() >")
Then call it like this:
def test():
send_command("xyz",3)
Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.