コード例 #1
0
def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 8000)
    print('connect to server {0}'.format(server_address))
    s.connect(server_address)
    msg = 'hello socket'
    print_green('sending data ==> ' + msg)
    s.send(msg)
    msg = s.recv(1024)
    print_red('recving data <== ' + msg)
コード例 #2
0
def main():
    # create tcp socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # enable reuse address port
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # bind socket to the port
    server_address = ('localhost', 8000)
    s.bind(server_address)
    s.listen(10)
    while 1:
        print_green(' waitting to recevie message from client')
        client, address = s.accept()
        t = threading.Thread(target=process_client_request, args=(client, ))
        t.daemon = True
        t.start()
コード例 #3
0
def main():
    # create tcp socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # enable reuse address port
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # bind socket to the port
    server_address = ('localhost', 8000)
    s.bind(server_address)
    s.listen(10)
    while 1:
        print_green(' waitting to recevie message from client')
        connection, address = s.accept()
        time.sleep(1)
        msg = connection.recv(1024)
        connection.send(msg.capitalize())
        connection.close()
コード例 #4
0
def main():
    # create tcp socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # enable reuse address port
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # bind socket to the port
    server_address = ('localhost', 8000)
    s.bind(server_address)
    s.listen(10)
    while 1:
        print_green(' waitting to recevie message from client')
        connection, address = s.accept()
        if not os.fork():  # enter child process
            time.sleep(1)  # exec task in 3 seconds
            msg = connection.recv(1024)
            print_red("child process")
            connection.send(msg.capitalize())
            connection.close()  # close client socket
            s.close()  # child does not need this
            break  # break child while loop
        connection.close()  # parent does not need this
コード例 #5
0
ファイル: interact.py プロジェクト: dongshengmu/interact
    def console(self, cmd='', expect=None, show_cmdbuf=True, rtn_cmdbuf=False):
        """Attach main program's console to the subprocess's console for its input and output.
        This allows user to manually interact with the subprocess. The subprocess console will be
        detached either by Ctrl-], or when the expect regex is found.

        - cmd: an optional command to be executed, default is an Enter key to display console prompt.
        - expect: a regex to expect, if found, detached from the subprocess and return (o, e).
        - show_cmdbuf: if True, and if exited with Ctrl-], print the manual steps done within the console session.
                Note, special keys (arrow, tab, delete, etc) are printed as raw.
        - rtn_cmdbuf: if True, return the intercepted user input as a string, together with (o, e)
        - return: return (o, e), the outputs from stdout and stderr,
                When exit with Ctrl-], return (o, e, cmdbuf) if rtn_cmdbuf is True, else None.
        """

        if not self.is_alive():
            self.print_warn('Connection %s dropped, reconnecting' % self.name)
            self._connect()
        util.print_header("Console attached to '%s'. Escape character is '^]'" % self.name)

        if cmd is not None:
            cmd = cmd.strip() + '\n'

        results = self.cmd_interact(cmd=cmd, expect=expect, timeout=self.FOREVER)

        if len(results) == 3:
            o, e, steps = results
            steps = steps.splitlines()
            util.print_header('Console detached from "%s", by Ctrl-]' % self.name)
            if show_cmdbuf:
                util.print_green("Here are the manual steps done in interactive console of %s:" % self.name)
                for x in steps:
                    util.print_green('    %s' % x.__repr__())
                print('')
            if rtn_cmdbuf:
                return o, e, steps
            return  # return None to keep interactive console clean.
        else:
            o, e = results
            util.print_header('Console detached from "%s"' % self.name)
            return o, e
コード例 #6
0
def main():
    # create tcp socket
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # enable reuse address port
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # bind socket to the port
    server.setblocking(0)
    server_address = ('localhost', 8000)
    server.bind(server_address)
    server.listen(10)
    # Outgoing message queues (socket:Queue)
    message_queues = {}
    # 1 second
    TIMEOUT = 1
    """
    POLLIN  Input ready
    POLLPRI Priority input ready
    POLLOUT Able to receive output
    POLLERR Error
    POLLHUP Channel closed
    POLLNVAL    Channel not open
    """
    READ_ONLY = select.POLLIN | select.POLLPRI | select.POLLHUP | select.POLLERR
    READ_WRITE = READ_ONLY | select.POLLOUT
    poller = select.epoll()
    poller.register(server.fileno(), READ_ONLY)

    # Map file descriptors to socket objects
    fd_to_socket = {server.fileno(): server}
    while True:
        # Wait for at least one of the sockets to be ready for processing
        print_green(' waitting to recevie message from client')
        events = poller.poll(TIMEOUT)
        for fd, flag in events:
            # Retrieve the actual socket from its file descriptor
            s = fd_to_socket[fd]
            if flag & (select.POLLIN | select.POLLPRI):
                if s is server:
                    # A "readable" server socket is ready to accept a connection
                    connection, client_address = s.accept()
                    connection.setblocking(0)
                    fd_to_socket[connection.fileno()] = connection
                    poller.register(connection.fileno(), READ_ONLY)

                    # Give the connection a queue for data we want to send
                    message_queues[connection] = Queue.Queue()
                    print_red(' connection {0} is comming ==> '.format(
                        connection.getpeername()))
                else:
                    data = s.recv(1024)
                    if data:
                        print_red(' connection {0} is sending ==> '.format(
                            s.getpeername()))
                        message_queues[s].put(data.capitalize())
                        # Add output channel for response
                        poller.modify(s, READ_WRITE)
                    else:
                        # Interpret empty result as closed connection
                        print_red(' connection {0} is closed ==> '.format(
                            s.getpeername()))
                        # Stop listening for input on the connection
                        poller.unregister(s)
                        s.close()

                        # Remove message queue
                        del message_queues[s]
            elif flag & select.POLLHUP:
                # Client hung up
                print_red('connection {0} is closing ==> '.format(
                    s.getpeername()))
                # Stop listening for input on the connection
                poller.unregister(s)
                s.close()
            elif flag & select.POLLOUT:
                # Socket is ready to send data, if there is any to send.
                try:
                    next_msg = message_queues[s].get_nowait()
                except Queue.Empty:
                    # No messages waiting so stop checking for writability.
                    poller.modify(s, READ_ONLY)
                else:
                    print_red('connection {0} sending msg ==> '.format(
                        s.getpeername()))
                    s.send(next_msg)
            elif flag & select.POLLERR:
                print_red('connection {0} exceptional ==> '.format(
                    s.getpeername()))
                # Stop listening for input on the connection
                poller.unregister(s)
                s.close()

                # Remove message queue
                del message_queues[s]
コード例 #7
0
    msg = s.recv(1024)
    print_red('recving data <== ' + msg)


def loop_n_times(n):
    for _ in range(n):
        main()
        time.sleep(3)


class ConcurrenceClient(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        loop_n_times(QS_P_THREAD)


if __name__ == '__main__':
    start = time.time()
    clients = []
    for _ in range(THREAD_COUNT):
        t = ConcurrenceClient()
        clients.append(t)
        t.start()

    for t in clients:
        t.join()

    print_green('time consume ==> ' + str(time.time() - start))
コード例 #8
0
ファイル: interact.py プロジェクト: dongshengmu/interact
    def cmd_batch(self, cmds, stop_on_error=False, hide_pass=False, precall=None, preargs=(), prekwargs=None,
                  postcall=None, postargs=(), postkwargs=None, return_pass_fail=False, *args, **kwargs):
        """Execute a list of commands, return a list of (cmd, stdout_output, stderr_output, result)

        - cmds: a list of command/dict, comment or (command/dict, pass_pattern) to be executed in the sequence order.
            For items of (command/dict, pass_pattern) the pass_pattern is matched in stdout_output
            and pass/fail is printed. The command can be a string command, or a dict of arguments
            for self.cmd() method, if it is a dict, the common args/kwargs won't be used.
            A string staring with '#' is a comment, and will be printed.

            Supported cmd format::

                '# comment' :
                    A comment to be printed.
                'cmd' :
                    Execute a cmd, with the common arguments specified in \*\*kwargs.
                dict(cmd='cmd', timeout=80, expect='xxx', ...) :
                    execute a cmd, with user specified arguments for the cmd.
                ('cmd' or dict, pass_pattern) :
                    Execute cmd or a dict cmd on, and
                    check the pass_pattern in command outputs.
                    - pass_pattern is a regex string, if found, the command is considered as passed.
                    - Command without pass_pattern specified is treated as pass.

        - stop_on_error: if a step fail, do not continue the rest steps.
        - hide_pass: do not print the pass message of pattern checking.
        - precall: python function to be called before each cmd, cmd execution will
            be skipped if precall returns False.

            - precall(cmd, results, \*preargs, \*\*prekwargs)
            - cmd: the next command to be executed.
            - results: a list of (cmd, o, e, r) of all preceeding steps.
            - eg: precall = lambda cmd, results: pause_for_a_key('press s to skip') != 's'
        - preargs: tuple arguments for precall
        - prekwargs: keyword arguments for precall
        - postcall: python function to be called after each cmd execution.
            - postcall(cmd, o, e, r, \*postargs, \*\*postargs)
            - cmd: the command had just executed
            - o, e: the stdout, stderr output from the command
            - r: the result whether the output has the regex pass_pattern.
                None if no pass_pattern specified for this command.
            - eg: postcall = lambda cmd, o, e, r: do_pass() if expected_result[cmd] in o else do_fail()
        - postargs: list of arguments for postcall
        - postkwargs: keyword arguments for postcall
        - return_pass_fail: if True, return False if any command execution does not have expected result.
        - return: a list of (cmd, o, e, r), or True/False if return_pass_fail is True.
        """
        if prekwargs is None:
            prekwargs = {}
        if postkwargs is None:
            postkwargs = {}
        results = []
        if isinstance(cmds, str):
            cmds = cmds.splitlines()
        for x in cmds:
            if isinstance(x, str) and x.startswith('#'):
                # this is a comment
                util.print_green(x)
                results.append((x, None, None, None))
                continue
            if isinstance(x, (list, tuple)):
                # this is a command with a pass_pattern
                cmd, pass_pattern = x
            else:
                cmd, pass_pattern = x, None
            if precall and not precall(cmd, results, *preargs, **prekwargs):
                results.append((cmd, None, None, None))
                continue
            # execute the command
            if isinstance(cmd, dict):
                # this is a dict, a command with its specfic arguments
                o, e = self.cmd(**cmd)
            else:
                # this is a command that uses the batch's common arguments
                o, e = self.cmd(cmd, *args, **kwargs)
            if pass_pattern:
                if re.search(pass_pattern, o):
                    r = True
                    if not hide_pass:
                        util.print_pass('pattern "%s" found in "%s" output' % (pass_pattern, cmd))
                else:
                    r = False
                    util.print_fail('pattern "%s" NOT found in "%s" output' % (pass_pattern, cmd))
            else:
                r = None
            results.append((cmd, o, e, r))
            if r is False and stop_on_error:
                return False if return_pass_fail else results
            if postcall:
                postcall(cmd, o, e, r, *postargs, **postkwargs)

        if return_pass_fail:
            results = [x for x in results if x[3] is False] == []
        return results
コード例 #9
0
def main():
    # create tcp socket
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # enable reuse address port
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # bind socket to the port
    server.setblocking(0)
    server_address = ('localhost', 8000)
    server.bind(server_address)
    server.listen(10)

    # Sockets from which we expect to read
    inputs = [server]
    # Sockets to which we expect to write
    outputs = []
    # Outgoing message queues (socket:Queue)
    message_queues = {}

    while inputs:

        # Wait for at least one of the sockets to be ready for processing
        print_green(' waitting to recevie message from client')
        readable, writable, exceptional = select.select(inputs, outputs, inputs)  # noqa

        # Handle inputs
        for s in readable:
            if s is server:
                # A "readable" server socket is ready to accept a connection
                connection, client_address = s.accept()
                connection.setblocking(0)
                # 新 socket 加入读监听列表
                inputs.append(connection)
                message_queues[connection] = Queue.Queue()
                print_red(' connection {0} is comming ==> '.format(connection.getpeername()))
            else:
                data = s.recv(1024)
                if data:
                    # 读取数据的 socket 加入输出列表
                    if s not in outputs:
                        outputs.append(s)
                    # 此 socket 数据积累
                    print_red(' connection {0} is sending ==> '.format(s.getpeername()))
                    message_queues[s].put(data.capitalize())
                else:
                    # client 关闭 socket
                    if s in outputs:
                        outputs.remove(s)
                    inputs.remove(s)
                    print_red(' connection {0} is closed ==> '.format(s.getpeername()))
                    s.close()

        for s in writable:
            try:
                next_msg = message_queues[s].get_nowait()
            except Queue.Empty:
                if s in outputs:
                    outputs.remove(s)
            else:
                s.send(next_msg)

        # Handle "exceptional conditions"
        for s in exceptional:
            inputs.remove(s)
            if s in outputs:
                outputs.remove(s)
            s.close()

            del message_queues[s]