test.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter core
  5. name:demeter.py
  6. author:rabin
  7. """
  8. import pexpect
  9. from pexpect import replwrap, EOF
  10. class MysqlWrapper(replwrap.REPLWrapper):
  11. def __init__(self, cmd_or_spawn, orig_prompt, prompt_change,
  12. extra_init_cmd=None, line_output_callback=None):
  13. self.line_output_callback = line_output_callback
  14. replwrap.REPLWrapper.__init__(self, cmd_or_spawn, orig_prompt,
  15. prompt_change, extra_init_cmd=extra_init_cmd)
  16. def _expect_prompts(self, timeout=-1):
  17. if timeout == None:
  18. while True:
  19. pos = self.child.expect_exact([self.prompt, self.continuation_prompt, u'\r\n'],
  20. timeout=None)
  21. if pos == 2:
  22. self.line_output_callback(self.child.before)
  23. else:
  24. if len(self.child.before) != 0:
  25. self.line_output_callback(self.child.before)
  26. break
  27. else:
  28. pos = replwrap.REPLWrapper._expect_prompt(self, timeout=timeout)
  29. return pos
  30. def output(content):
  31. print content
  32. if __name__ == '__main__':
  33. user = 'root'
  34. ip = '192.168.15.10'
  35. port = '3309'
  36. password = '123456'
  37. child = pexpect.spawn('mysql -u%s -h%s -P%s -p%s' % (user, ip, port,password))
  38. py = MysqlWrapper(child, u"mysql>", None,extra_init_cmd=None,line_output_callback=output)
  39. #py.run_command("show databases;", timeout=None)
  40. outout = py.run_command("use dever;select * from plant_forum_posts;", timeout=None)
  41. print py.child.before
  42. """
  43. child = pexpect.spawn('mysql -u%s -h%s -P%s -p' % (user, ip, port))
  44. child.expect ('password:')
  45. child.sendline (password)
  46. child.expect('mysql>')
  47. child.sendline('show databases;')
  48. child.expect('mysql>')
  49. child.sendline('use dever;')
  50. print child.after # Print the result of the ls command.
  51. #child.interact() # Give control of the child to the user.
  52. """