2

I have the following python code

import ctypes
import sys
dll = ctypes.CDLL('./file.so')
if __name__ == "__main__":
    dll.myFunction()

The function myFunction waits for a stdin input, so my question is: how can I simulate this input using python?

Mel
  • 5,837
  • 10
  • 37
  • 42
user2563949
  • 151
  • 1
  • 9
  • This may help: http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user – PM 2Ring Mar 24 '16 at 10:21
  • 1
    A solution you'll find in many variations is to temporarily replace the current stdin with a pipe. Start by saving a copy of the current stdin, e.g. `old_stdin = os.dup(sys.stdin.fileno())`. Then create a pipe, e.g. `fd_read, fd_write = os.pipe()`, and set the read end as the new stdin, e.g. `os.dup2(fd_read, sys.stdin.fileno())`. Before calling `myFunction`, write to `fd_write` via `os.write`. After calling the function, restore the original stdin using `os.dup2(old_stdin, sys.stdin.fileno())` and close `old_stdin` and the pipe file descriptors via `os.close`. – Eryk Sun Mar 24 '16 at 10:58

2 Answers2

0

maybe you could use xautomation http://xautomation.sourceforge.net/keyboard.html

0

I think it could be required for app automation tasks so I could recommend PyUserInput package for input simulation:

from pykeyboard import PyKeyboard

kbd = PyKeyboard()
kbd.tap_key('h')
kbd.tap_key('i')

Example for windows users (althogh PyUserInput is corssplatform for Windows it is built atop pywin32):

import time
import win32com.client as comclt
wsh = comclt.Dispatch("WScript.Shell")
wsh.Run("calc")
time.sleep(1)
wsh.AppActivate("Calculator")

from pykeyboard import PyKeyboard 
kbd = PyKeyboard()
kbd.type_string(["1"]+["0"]*6,interval=1)
rook
  • 5,880
  • 4
  • 39
  • 51