import time

from lxml import html

import zmq

c = zmq.Context()
in_socket = c.socket(zmq.PULL)
in_socket.setsockopt(zmq.HWM, 1)
in_socket.connect('tcp://127.0.0.1:5555')

out_socket = c.socket(zmq.PUSH)
out_socket.connect('tcp://127.0.0.1:5556')

print 'Started'
while True:
    try:
        url = in_socket.recv()
        time.sleep(0.5)
        try:
            dom = html.parse(url)
            title = dom.getroot().find('head/title').text
        except IOError:
            title = '404'

        print '{}'.format(url, title)

        out_socket.send(title)
    except KeyboardInterrupt:
        break
