Pingback – pinging other sites in django
November 16, 2009 | frameworks, pythonauthor: Karol Zielinski | comments: 0 | views: 1862
Tags: blog, django, linkback, ping, pingback, python
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.
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
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.
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.