... because from time to time I'm a web developer, too
About me
Projects
Contact
Links

Pingback – pinging other sites in django

November 16, 2009 | frameworks, python
author: Karol Zielinski | comments: 0 | views: 1862
Tags: , , , , ,

One of our blogs has been created in django. Now… because of its increasing popularity – we need to support pingbacks in this blog. That’s why we need to create functionality to handle it. This time… I will describe how to create functionality to pinging other websites via django.

What is pingback?

A pingback is one of three types of linkbacks, methods for Web authors to request notification when somebody links to one of their documents. This enables authors to keep track of who is linking to, or referring to their articles. Some weblog software, such as Movable Type, Serendipity, WordPress and Telligent Community, support automatic pingbacks where all the links in a published article can be pinged when the article is published.

Essentially, a pingback is an XML-RPC request (not to be confused with an ICMP ping) sent from Site A to Site B, when an author of the blog at Site A writes a post that links to Site B. However, it also requires a hyperlink. When Site B receives the notification signal, it automatically goes back to Site A checking for the existence of a live incoming link. If that link exists, the pingback is recorded successfully. This makes pingbacks less prone to spam than trackbacks. Pingback-enabled resources must either use an X-Pingback header or contain a element to the XML-RPC script.

from http://en.wikipedia.org/wiki/Pingback

Let’s build something…

Ok, what we want to do is: create functionality, which will ping websites while we are creating or changing news in our blog. I don’t want to ping all the websites, which URLs I’m putting in article. I want to have dedicated column in my table for newses in the database. All the websites I want to ping are semicolon-separated.

Let’s do it.

First… create method, which will find pingback link in the website.

cd /path_to_our_project
vim pingback.py
import urllib2, re

def get_pingurl(trackback_urls):
    PINGBACK_RE = re.compile('
')
    pingurl = None
    splitted_urls = trackback_urls.split(';')
    urls = []

    if splitted_urls:
        for url in splitted_urls:
            urls_dict = {}
            url = url.strip()
            if not url:
                continue

            urls_dict['url'] = url

            try:
                remote = urllib2.urlopen(url)
            except urllib2.URLError:
                continue

            pingurl = None
            try:
                # first look for a X-Pingback header
                pingurl = remote.info().getheader('X-Pingback')
            except:
                continue

            if pingurl:
                urls_dict['pingurl'] = pingurl
                urls.append(urls_dict)
                continue

            try:
                # then try to find a
 element
                pingurl = PINGBACK_RE.findall(remote.read())
                pingurl = pingurl[0]
            except:
                continue

            if pingurl:
                urls_dict['pingurl'] = pingurl
                urls.append(urls_dict)
                continue

    return urls

next… we need to ping all the websites.

vim main/models.py

add method ‘save()‘ to class News:

	def save(self):
		super(News, self).save()

		from xmlrpclib import ServerProxy

		if self.trackback_urls:
			ping_urls = get_pingurl(self.trackback_urls)

			if ping_urls:
				link_to_this_news = 'http://this_is_our_domain.com/news/' + str(self.slug)
				for ping_url in ping_urls:
					try:
						proxy = ServerProxy(ping_url['pingurl'])
						proxy.pingback.ping(link_to_this_news, ping_url['url'])
					except Exception:
						continue

trackback_urls – semicolon-separated URLs to ping
slug – unique slug for our news

The end.

Next time… I will try to describe how to receive pings.

Bookmark and Share
Post Pingback – pinging other sites in django to develway Post Pingback – pinging other sites in django to Delicious Post Pingback – pinging other sites in django to Digg Post Pingback – pinging other sites in django to Facebook Post Pingback – pinging other sites in django to Reddit Post Pingback – pinging other sites in django to StumbleUpon

Related news and resources

Write a comment

Karol Zielinski :: Just a tech stuff Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.

Most popular posts

Much more links

Karol Zielinski    |   contact me
Gdynia, Poland
RSS - Just a tech stuff - python, java blog - web development blog Karol Zielinski on twitter Karol Zielinski on LinkedIn Karol Zielinski on facebook Karol Zielinski on delicious Karol Zielinski on digg Karol Zielinski on flickr Karol Zielinski on stumbleupon Karol Zielinski on technorati