my_boyfriend.py
def forward(receive_sock, send_sock, processing_q, result_q, connection_id):
    """
    Handles one direction of communication in a connection. For processing messages
    in order, see the process_messages() function. This implementation assumes the
    server is finicky and will close the connection if a message is not promptly sent
    after connection - to allow you editing time, we therefore wait to open
    the TCP connection until after editing. Assumes the client is patient.
    """
    return_sock = False
    # If we haven't opened the forwarding connection yet, send_sock is None. So we have
    # to write our select() call to avoid passing None to it. This never gets called
    # with receive_sock as None so we don't worry about it.
    readable, writable, exception = select.select(
        [receive_sock],                                                          # rlist
        [send_sock] if send_sock is not None else [],                            # wlist
        [receive_sock, send_sock] if send_sock is not None else [receive_sock],  # xlist
        0  # timeout disabled - don't block
    )

    if len(exception) > 0:
        processing_q.put(("Err", "Exception in socket(s): " + str(exception)))
        return True  # Something bad happened, close the sockets.

    if receive_sock in readable and (send_sock in writable or send_sock is None):
        data_bytes = receive_sock.recv(args.read_size)
        if len(data_bytes) == 0:  # Signifies connection is closed on the remote end.
            return True
        processing_q.put((connection_id, data_bytes))
        try:
            data_string = result_q.get()  # Blocks until processed message available

            if send_sock is None:  # We need to open a connection
                try:
                    return_sock = True
                    send_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
                    send_sock.connect((args.server_ip, args.server_port))
                    send_sock.setblocking(False)
                except:
                    processing_q.put(("Err", "Error setting up forwarder.\n"
                                             + traceback.format_exc()))
                    return True  # Can't send data without this, so give up.

            try:
                data_bytes = ast.literal_eval(data_string)  # Convert from string to bytes
            except:
                processing_q.put(("Err", "Error parsing processed message.\n"
                                         + traceback.format_exc()))
        finally:
            try:
                sent = 0
                while sent < len(data_bytes):
                    sent += send_sock.send(data_bytes[sent:])
            except:
                processing_q.put(("Err", "Error forwarding message to destination.\n"
                                         + traceback.format_exc()))
                send_sock.close()  # Might need to do this here if it failed immediately
                return True  # Probably want to give up if sending failed somehow.
    if return_sock:  # Disgusting overloading of return value type
        return send_sock
    else:
        return False

    Handles one direction of communication in a connection. For processing messages
    in order, see the process_messages() function. This implementation assumes the
    server is finicky and will close the connection if a message is not promptly sent
    after connection - to allow you editing time, we therefore wait to open
    the TCP connection until after editing. Assumes the client is patient.
    
Handles one direction of communication in a connection. For processing messages
in order, see the process_messages() function. This implementation assumes the
server is finicky and will close the connection if a message is not promptly sent
after connection - to allow you editing time, we therefore wait to open
the TCP connection until after editing. Assumes the client is patient.
Handles one direction of communication in a connection . For processing messages in order see the process_messages () function . This implementation assumes the server is finicky and will close the connection if a message is not promptly sent after connection - to allow you editing time we therefore wait to open the TCP connection until after editing . Assumes the client is patient .
def forward(receive_sock, send_sock, processing_q, result_q, connection_id):
    return_sock = False
    readable, writable, exception = select.select(
        [receive_sock],                                                          
        [send_sock] if send_sock is not None else [],                            
        [receive_sock, send_sock] if send_sock is not None else [receive_sock],  
        0  
    )
    if len(exception) > 0:
        processing_q.put(("Err", "Exception in socket(s): " + str(exception)))
        return True  
    if receive_sock in readable and (send_sock in writable or send_sock is None):
        data_bytes = receive_sock.recv(args.read_size)
        if len(data_bytes) == 0:  
            return True
        processing_q.put((connection_id, data_bytes))
        try:
            data_string = result_q.get()  
            if send_sock is None:  
                try:
                    return_sock = True
                    send_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
                    send_sock.connect((args.server_ip, args.server_port))
                    send_sock.setblocking(False)
                except:
                    processing_q.put(("Err", "Error setting up forwarder.\n"
                                             + traceback.format_exc()))
                    return True  
            try:
                data_bytes = ast.literal_eval(data_string)  
            except:
                processing_q.put(("Err", "Error parsing processed message.\n"
                                         + traceback.format_exc()))
        finally:
            try:
                sent = 0
                while sent < len(data_bytes):
                    sent += send_sock.send(data_bytes[sent:])
            except:
                processing_q.put(("Err", "Error forwarding message to destination.\n"
                                         + traceback.format_exc()))
                send_sock.close()  
                return True  
    if return_sock:  
        return send_sock
    else:
        return False
def forward ( receive_sock , send_sock , processing_q , result_q , connection_id ) : return_sock = False readable , writable , exception = select . select ( [ receive_sock ] , [ send_sock ] if send_sock is not None else [ ] , [ receive_sock , send_sock ] if send_sock is not None else [ receive_sock ] , 0 ) if len ( exception ) > 0 : processing_q . put ( ( "Err" , "Exception in socket(s): " + str ( exception ) ) ) return True if receive_sock in readable and ( send_sock in writable or send_sock is None ) : data_bytes = receive_sock . recv ( args . read_size ) if len ( data_bytes ) == 0 : return True processing_q . put ( ( connection_id , data_bytes ) ) try : data_string = result_q . get ( ) if send_sock is None : try : return_sock = True send_sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM , 0 ) send_sock . connect ( ( args . server_ip , args . server_port ) ) send_sock . setblocking ( False ) except : processing_q . put ( ( "Err" , "Error setting up forwarder.\n" + traceback . format_exc ( ) ) ) return True try : data_bytes = ast . literal_eval ( data_string ) except : processing_q . put ( ( "Err" , "Error parsing processed message.\n" + traceback . format_exc ( ) ) ) finally : try : sent = 0 while sent < len ( data_bytes ) : sent += send_sock . send ( data_bytes [ sent : ] ) except : processing_q . put ( ( "Err" , "Error forwarding message to destination.\n" + traceback . format_exc ( ) ) ) send_sock . close ( ) return True if return_sock : return send_sock else : return False
Handles one direction of communication in a connection.
Handles one direction of communication in a connection .
"""
    Handles one direction of communication in a connection. For processing messages
    in order, see the process_messages() function. This implementation assumes the
    server is finicky and will close the connection if a message is not promptly sent
    after connection - to allow you editing time, we therefore wait to open
    the TCP connection until after editing. Assumes the client is patient.
    """ # If we haven't opened the forwarding connection yet, send_sock is None. So we have # to write our select() call to avoid passing None to it. This never gets called # with receive_sock as None so we don't worry about it. # rlist # wlist # xlist # timeout disabled - don't block # Something bad happened, close the sockets. # Signifies connection is closed on the remote end. # Blocks until processed message available # We need to open a connection # Can't send data without this, so give up. # Convert from string to bytes # Might need to do this here if it failed immediately # Probably want to give up if sending failed somehow. # Disgusting overloading of return value type
receive_sock
send_sock
processing_q
result_q
connection_id