Esempio n. 1
0
#!/usr/bin/env python
# coding=utf-8
import json
import commands
from nbNet import *

def cmdRunner(input):
    cmd_ret = commands.getstatusoutput(input)
    return json.dumps({'ret':cmd_ret[0], 'out':cmd_ret[1]}, separators=(',', ':'))

HOST = '0.0.0.0'
PORT = 8899

server = nbNet(HOST, PORT, cmdRunner)
server.run()
Esempio n. 2
0
        if write_ret == "writemore":
            pass
            # 写数据完成,重置各种计数器,开始等待新请求过来
        elif write_ret == "writecomplete":
            sock_state = self.conn_state[fd]
            conn = sock_state.sock_obj
            self.setFd(conn)
            self.conn_state[fd].state = "read"
            self.epoll_sock.modify(fd, select.EPOLLIN)
        elif write_ret == "closing":
            # 发生错误直接关闭,做到快速失败
            dbgPrint(msg)
            self.conn_state[fd].state = 'closing'
            # closing directly when error.
            self.state_machine(fd)


if __name__ == '__main__':
    # 这个是我们演示用的“业务逻辑”,做的事情就是将请求的数据反转
    # 例如:
    #   收到:0000000005HELLO
    #   回应:0000000005OLLEH
    def logic(d_in):
        return (d_in[::-1])

    # 监听在0.0.0.0:9076
    reverseD = nbNet('0.0.0.0', 9099, logic)

    # 状态机开始运行,除非被kill,否则永不退出
    reverseD.run()
Esempio n. 3
0
#!/usr/bin/env python
# coding=utf-8
import json
import commands
from nbNet import *


def cmdRunner(input):
    cmd_ret = commands.getstatusoutput(input)
    return json.dumps({
        'ret': cmd_ret[0],
        'out': cmd_ret[1]
    },
                      separators=(',', ':'))


HOST = '0.0.0.0'
PORT = 8899

server = nbNet(HOST, PORT, cmdRunner)
server.run()
Esempio n. 4
0
      pass
      # 写数据完成,重置各种计数器,开始等待新请求过来
    elif write_ret == "writecomplete":
      sock_state = self.conn_state[fd]
      conn = sock_state.sock_obj
      self.setFd(conn)
      self.conn_state[fd].state = "read"
      self.epoll_sock.modify(fd, select.EPOLLIN)
    elif write_ret == "closing":
      # 发生错误直接关闭,做到快速失败
      dbgPrint(msg)
      self.conn_state[fd].state = 'closing'
      # closing directly when error.
      self.state_machine(fd)


if __name__ == '__main__':
  # 这个是我们演示用的“业务逻辑”,做的事情就是将请求的数据反转
  # 例如:
  #   收到:0000000005HELLO
  #   回应:0000000005OLLEH
  def logic(d_in):
    return(d_in[::-1])

  # 监听在0.0.0.0:9076 
  reverseD = nbNet('0.0.0.0', 9099, logic)

  # 状态机开始运行,除非被kill,否则永不退出
  reverseD.run()