How to sign a transaction with multiple inputs? I have been able to program an application in python that sends valid transactions that spend 1 input, but I really can't get it to work with 2 or more inputs.

So my question is, how exactly is the hash that has to be signed for each input calculated?

What I have been doing so far, is, for example, if we have 2 inputs, we generate a transaction that spends only that first input, and thats the hash that has to be signed for the first input. And the same with the second input, we generate a transaction that only spends that second input. Both transactions need to have the same outputs.

Is that correct? For example, we want to spend input aaaa:0 and input bbbb:1 and we want to send them to some address:

To calculate the hash that the first input has to sign, we need to generate this transaction:

'version': 1,
'inputs': (1)
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0


And to calculate the hash that the second input has to sign:

'version': 1,
'inputs': (1)
    'output_tx_hash': 'bbbb', # this changes
    'output_position': 1,     # this changes
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0


We serialize them, add them 01000000 (SIGHASH_ALL), double sha256 that, sign it, and add 01. That way we get the two signatures that we will use on each input respectively. My application does exactly that and doesn't work. What am I missing?
I found the way to do it, so, if anyone is interested, here is how to do it:

When you have more than 1 input, you don't have to remove the inputs that you are not going to sign, you have to remove only their scripts. So, if you want to sign the transaction posted in the question, the first hash would be calculated like so:

'version': 1,
'inputs': (2)
    {
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
    }, {
    'output_tx_hash': 'bbbb',
    'output_position': 1,
    'script': '', # Nothing
    'sequence': ffffffff,
    }
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0


And the second hash:

'version': 1,
'inputs': (2)
    {
    'output_tx_hash': 'aaaa',
    'output_position': 0,
    'script': '', # Nothing
    'sequence': ffffffff,
    }, {
    'output_tx_hash': 'bbbb',
    'output_position': 1,
    'script': the original script, like: '76a914' + hash + '88ac',
    'sequence': ffffffff,
    }
'outputs': (1)
    'value' : 100000
    'script' : '76a914' + hash of btc pub key + '88ac'
'locktime': 0


Hope it helps anyone.