1

Due to memory constraints I need to convert some Python scripts to C. In my script they used a module called "pexpect" which allows spawning child applications and controlling them automatically. Here I am including a piece of code from a Class. Any leads appreciated.

from pexpect import spawn, EOF, run

def do_transfer(self):
    ret_status = DEFAULT_RETURN_VAL
    count = 0
    while ret_status != 0 and count < self.retry:
        if self.user_name and self.password:
            cmd = "%s --connect-timeout %s %s %s -o %s -u %s:%s"%(CURL_CMD, self.conn_timeout, DEFAULT_OPTS,
                                                                  self.remote_url, self.local_path, self.user_name, self.password)
        else:
            cmd = "%s --connect-timeout %s %s %s -o %s"%(CURL_CMD, self.conn_timeout, DEFAULT_OPTS, self.remote_url, self.local_path)           
        transfer = spawn(cmd)
        transfer.expect(EOF, timeout=self.transfer_timeout)
        transfer.close()
        if transfer.exitstatus is not None:
            ret_status = transfer.exitstatus
        elif transfer.signalstatus is not None:
            ret_status = transfer.signalstatus

        del transfer
        count += 1
        sleep(3)

    return ret_status
Jens
  • 69,818
  • 15
  • 125
  • 179
  • 1
    The pexpect module is a Python wrapper above the C Expect library. IMHO if it can be installed on your system, conversion will be much simpler... – Serge Ballesta Jul 03 '20 at 12:55
  • 2
    Did you read what I have written? Expect is a **C** library and does not depend on Python. – Serge Ballesta Jul 03 '20 at 13:13
  • Thanks for your reply, I found expect library in C but finding difficulty in implementing in my script. https://linux.die.net/man/3/libexpect – Niranjan Mahesh Jul 06 '20 at 07:42

0 Answers0