listings / pingback.py
import re
from urllib.request import urlopen
from xmlrpc.client import ServerProxy, Error
from xml.parsers.expat import ExpatError
def ping(source_url, target_url):
    '''
    Makes a pingback request to target_url on behalf of source_url, i.e.
    effectively saying to target_url that "the page at source_url is
    linking to you".
    '''
    def search_link(f):
        content = f.read(512 * 1024)
        match = re.search(rb'<link rel="pingback" href="([^"]+)" ?/?>', content)
        return match and match.group(1).decode('utf-8')
    request_url = 'http:%s' % target_url if target_url.startswith('//') else target_url
    f = urlopen(request_url)
    try:
        info = f.info()
        server_url = info.get('X-Pingback', '') or search_link(f)
        if server_url:
            server = ServerProxy(server_url)
            server.pingback.ping(source_url, target_url)
    finally:
        f.close()
def ping_urls(source_url, target_url_list):
    '''
    Makes pingback requests to all links in url list.
    source_url is a URL of the page contaning HTML fragment.
    '''
    for url in target_url_list:
        try:
            ping(source_url, url)
        except (IOError, Error, ExpatError):
            # One failed URL shouldn't block others
            pass