Skip to content

Posts tagged ‘python’

4
Jun

Django with JsonRPC and XMLRPC

[Edit: take a look at RPC4Django for a JSONRPC and XMLRPC server for Django]

I corresponded recently with a developer working on a Django-powered jsonrpc library. In the past, I have done some work on web applications that require good external interfaces. In some cases, however, it makes sense to make the same methods available via both jsonrpc and xmlrpc.

For javascript and flash, json makes a lot of sense. For communication between client side and server side, jsonrpc works very well since json is natively supported and speed can be more of a factor in presentation. However, if external interfaces are also going to interact with your API, jsonrpc is not as well supported as xmlrpc. Virtually every language has good libraries for xmlrpc. For this reason, it makes good sense to combine the two and make the same methods available to both.

import logging
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from django.http import HttpResponse, Http404
from jsonrpc import JsonRpc

xmlrpcdispatcher = SimpleXMLRPCDispatcher(allow_none=False, encoding=None)
jsonrpcdispatcher = JsonRpc()
# ... register methods with xmlrpc and jsonrpc

def rpc_handler(request):
    if request.META['CONTENT_TYPE'] == 'application/json':
        response = jsonrpcdispatcher.handle_request(request)
    elif request.META['CONTENT_TYPE'] == 'text/xml':
        response = HttpResponse()
        response.write(xmlrpcdispatcher._marshaled_dispatch(request.raw_post_data))
        response['Content-length'] = str(len(response.content))
    else:
        # display documentation, or just raise a 404
        logging.debug('rpc request type: %s' %request.META['CONTENT_TYPE'])
        raise Http404

    return response
30
May

Subversion Backups via Email

I wrote a simple subversion backup script that runs on my hosting provider, webfaction, and backs up my subversion repository. I have this script running in cron and sending periodic backups to gmail. However, it should work on any unix based system with python, gzip, and subversion. Simply set the SMTP settings (configured in the webfaction panel if you have webfaction), your email information and the path to subversion and run the script.

This script requires python 2.5 or 2.6. It will require modification under python 2.4.


!/usr/bin/env python2.6

import os
import smtplib
import time
import mimetypes
from datetime import datetime

# python 2.5/2.6 is required because the email library changed after 2.4
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase

# SMTP settings
SMTPUSER = 'YOURSMTPUSERNAME'
SMTPPASS = 'YOURSMTPPASSOWRD'
SMTPSERVER = 'smtp.webfaction.com'
SMTPPORT = 25

# who to address the email to and from
TO = 'YOUREMAIL@EXAMPLE.com'
FROM = 'backups@webfaction.com'

# svn location
SVN_LOCATION = 'PATH_TO_SVN_DIR'

# these probably do not need to be changed
SVN_BACKUP_FILE = 'svn.dump'
GZIP_BACKUP_FILE = SVN_BACKUP_FILE + '.gz'
BACKUP_LOCATION = '/tmp/'+SVN_BACKUP_FILE
ZIPPED_BACKUP = BACKUP_LOCATION+'.gz'
BACKUP_CMD = 'svnadmin dump '+SVN_LOCATION+' > ' + BACKUP_LOCATION
GZIP_CMD = 'gzip -f '+BACKUP_LOCATION

print '*******************************************************'
print '** Backing up repository'
print '*******************************************************'
os.system(BACKUP_CMD)

print '*******************************************************'
print '** Zipping backup'
print '*******************************************************'
os.system(GZIP_CMD)

print '*******************************************************'
print '** Emailing backup'
print '*******************************************************'
msg = MIMEMultipart()
msg['Subject'] = 'Subversion Backup '+str(datetime.now())
msg['From'] = FROM
msg['To'] = list().append(TO)
msg.preample = 'Should not see this in a MIME-aware mail reader.\n'

# add the gzipped attachment
fp = open(ZIPPED_BACKUP, 'rb')
att = MIMEBase('application', 'gzip')
att.set_payload(fp.read())
encoders.encode_base64(att)
att.add_header('Content-Disposition', 'attachment', filename=GZIP_BACKUP_FILE)
fp.close()
msg.attach(att)

# Send the email via our own SMTP server.
s = smtplib.SMTP(SMTPSERVER, SMTPPORT)
s.ehlo()
s.starttls()
s.ehlo()
s.login(SMTPUSER, SMTPPASS)
s.sendmail(FROM, TO, msg.as_string())
s.quit()

# removing backup
os.remove(ZIPPED_BACKUP)

In order to use gmail’s smtp server, change your settings like so:

SMTPUSER = 'YOURGMAILUSERNAME'
SMTPPASS = 'YOURGMAILPASSOWRD'
SMTPSERVER = 'smtp.gmail.com'
SMTPPORT = 587      # note the port change!