[Python] expect: make two python scripts communicate

Discussão em 'Python' iniciado por Stack, Novembro 23, 2024.

  1. Stack

    Stack Membro Participativo

    I want to have two scripts communicating by exchanging messages. I have to use pexpect because of other restrictions. I am trying to make a minimal working example before I build it out for my application.

    I have tried to do a minimal working example by following the tutorials I could find on the internet. But I've failed and here is my attempt.The first script is the one that initiates communication:

    #script_1.py

    import pexpect

    p = pexpect.spawn("python /path/to/script/script_2.py")
    p.expect(">")
    p.sendline(input("Message to the other script: "))
    print( p.before )


    Here is the second script which should receive communication and send answer:

    #script_2.py

    indata = input(">")
    print(indata)


    How can I make two python scripts communicate by using pexpect?


    EDIT: I was asked why I say that the scripts fail given that there are no error messages. The reason it is a failure is that script 2 should echo the message sent by script 1, and script 1 should print out (or in other ways capture the response), but that doesn't happen

    Continue reading...

Compartilhe esta Página