from gi.repository import Soup, GLib, Gio
import sys

session = Soup.Session()

def sent(session, task, data):
    print('Cancelling the first request')
    session.cancel_message(message, Soup.Status.CANCELLED)
    datastream = session.send_finish(task)
    # Note how I do not read content from current request even though I've
    # cancelled the request
    # P.S. Code below would take care of reading.
    # while datastream.read_bytes(512, None).get_size(): print('-', end="")
    # print('.')
    datastream.close(None)

    print('Executing the second one')
    msg = Soup.Message.new('GET', 'http://httpbin.org/html')
    session.send_async(msg, None, sent2, None)

def sent2(session, task, data):
    # Will fail here.
    datastream = session.send_finish(task)
    sys.exit(0)

message = Soup.Message.new('GET', 'http://httpbin.org/status/418')
session.send_async(message, None, sent, None)

# Main loop
ctx = GLib.main_context_default()
while True:
    ctx.iteration()
