示例#1
0
def input_int(msg=''):
    while True:
        valor_lido = input(msg)
        if valor_lido.isdigit():
            return int(valor_lido)
        else:
            print_red('Erro! Digite um número inteiro válido.', flush=True)
示例#2
0
def process_client_request(client):
    time.sleep(1)  # exec task in 3 seconds
    msg = client.recv(1024)
    print_red("thread %s" % threading.current_thread().name)
    client.send(msg.capitalize())
    client.close()  # close client socket
    with lock:
        GLOBAL_REQUEST_STATE['count'] += 1
示例#3
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)
示例#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
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]
示例#6
0
def process_client_request(client):
    time.sleep(1)  # exec task in 3 seconds
    msg = client.recv(1024)
    print_red("thread %s" % threading.current_thread().name)
    client.send(msg.capitalize())
    client.close()  # close client socket
示例#7
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]