Source

green380 / examples / http_get.py

Full commit
#!/usr/bin/env python3.3

import collections
import pdb
import sys
import urllib.parse
import green380

def http_get(url, timeout=None):
    @green380.spawn
    def timeout_routine():
        yield from green380.sleep(timeout)
        yield from ch.send()
    split = urllib.parse.urlsplit(url)
    sock = green380.Socket()
    addr = split.netloc.split(':')
    if len(addr) == 1:
        addr.append(80)
    else:
        addr[-1] = int(addr[-1])
    addr = tuple(addr)
    @green380.spawn
    def connect_routine():
        yield from sock.connect(addr)
        yield from ch.send()
    ch = green380.Channel()
    value, sender = yield from ch.recv_from()
    assert value is None, value
    if sender is timeout_routine:
        raise Exception('timed out')
    elif sender is not connect_routine:
        assert False, sender
    @green380.spawn
    def send_header():
        return sock.sendall(
            'GET {split[2]}?{split[3]} HTTP/1.1\r\nHost: {addr[0]}\r\nConnection: close\r\n\r\n'.format(
                split=split, addr=addr).encode())
    response_header_lines = (yield from sock.recv_until(b'\r\n\r\n')).split(b'\r\n')
    headers = collections.defaultdict(list)
    for l in map(bytes.decode, response_header_lines[1:]):
        if not l:
            continue
        h, *v = l.split(':', 1)
        headers[h.lower().strip()].append(v[0])
    return response_header_lines[0].decode(), headers, sock

def main():
    for url in sys.argv[1:]:
        status, headers, sock = yield from http_get(url, 0.1)
        print(status, file=sys.stderr)
        print(headers, file=sys.stderr)
        while True:
            buf = yield from sock.recv(0x1000)
            if not buf:
                break
            sys.stdout.buffer.write(buf)
        sys.stdout.flush()

if __name__ == '__main__':
    green380.spawn(main)
    green380.run()