Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4972041
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2012-03-26 12:34:20

1.main.py

  1. #!/bin/env python
  2. #-*-coding:utf-8-*-

  3. import sys
  4. from twisted.python import log
  5. log.startLogging(sys.stdout)

  6. from twisted.internet import protocol, reactor

  7. class SubProcessProtocol(protocol.ProcessProtocol):
  8.     def __init__(self):
  9.         self.input = 97

  10.     def connectionMade(self):
  11.         log.msg('Connection Made.')
  12.         self.send()

  13.     def send(self):
  14.         self.transport.write(chr(self.input) + "\n")
  15.         log.msg('Sent:', chr(self.input))
  16.         self.input += 1

  17.     def outReceived(self, data):
  18.         log.msg('Received:', data)
  19.         reactor.callLater(.1, self.send)

  20.     def errReceived(self, data):
  21.         log.msg('Error:', data)

  22.     def inConnectionLost(self):
  23.         log.msg('Error: inConnectionLost')

  24.     def outConnectionLost(self):
  25.         log.msg('Error: inConnectionLost')

  26.     def errConnectionLost(self):
  27.         log.msg('Error: errConnectionLost')

  28.     def proces***ited(self, reason):
  29.         log.msg('Process exit status:',reason.value.exitCode )

  30.     def processEnded(self, reason):
  31.         log.msg('Process end status:',reason.value.exitCode )
  32.         if reactor.running:
  33.             reactor.stop()

  34. def main():
  35.     p = SubProcessProtocol()
  36.     reactor.spawnProcess(p, 'sub.py', ['sub.py'], {})
  37.     reactor.run()

  38. if __name__ == "__main__":
  39.     main()

2. sub.py:

  1. #!/bin/env python
  2. #-*-coding:utf-8-*-

  3. import sys, os
  4. sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)


  5. def main():
  6.     running = True

  7.     while running:
  8.         input = sys.stdin.readline().rstrip('\n')
  9.         if input == 'q':
  10.             running = False
  11.         print "You said:%s" % input

  12. if __name__ == "__main__":
  13.     main()
  14.     sys.exit(2)
文章来自:http://www.cnitblog.com/donne/archive/2011/11/16/76327.html
阅读(1587) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~