Nidelven IT - All about Python, Zope & Plone - and Open Source!

Here you'll find issues related to our services. Mostly about Python, Zope and Plone, as well as hosting-related issues.

"Keeping IT real"






Older entries



Atom - Subscribe - Categories
Previous | Next

A walk up the pyramid

So, this Sunday I decided I could play a little around with new technology that I haven't tried much. I've worked with Zope and Plone for years, and it is important stay somewhat in touch with other Python web frameworks - to seize opportunities there as well.

I've seen some buzz about the Pyramid project and especially that it is very promising technically, and lightweight (when something says light-weight in software I think OK, somebody knows what they want and are doing).

So, first thing was to install a fresh Python, as the standard Python setup didn't include easy_install. Installed Python, installed ez_setup.py and then did a 'easy_install -u pyramid'. So far so good. :)

OK, so onto testing this thing out, I followed the first example in this page:

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch...

and modified it so that the server was running on port 1234, because other ports were taken.

Accessing

http://blogologue.com:1234/hello/man

Gave the result

Hello man!

and yes, that's how easy it was to get up and running. :)

For a while now I've been contemplating building a simple URL shortening service (yes late to the party I know), so I decided I could try to create such a service using Pyramid.

I ended up with this code:

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid import httpexceptions

global urls
urls = []

def redirect(request):
    raise httpexceptions.HTTPFound(urls[int(request.matchdict['index'])])

def index(request):
    try:
        add_url = request.params['b']
        referer = request.referer
        global urls
        urls.append(referer)
        return Response("""Shortened URL: http://blogologue.com:1234/r%s""" %\
                        (len(urls)-1))
    except KeyError:
        return Response("""<html><head><title>Redirect service</title></head>   
    <body><h1>Redirect services</h1>                                            
    <p>Hello there, welcome :)</p>                                              
                                                                                
    <p>To get started using this URL shorting service,                          
    please drag the following link to your bookmark                             
    bar, and click on the link on whatever page you'd                           
    like bookmarked and accessible via this URL shortening                      
    service.</p>                                                                
                                                                                
    <p><a href="javascript:document.location='http://blogologue.com:1234/?b'"   
         >Shorten URL</a></p>                                                   
    </body>                                                                     
    </html>""")

if __name__ == '__main__':
    config = Configurator()
    config.add_route('index', '/')
    config.add_view(index, route_name='index')
    config.add_route('redirect', '/r{index}')
    config.add_view(redirect, route_name='redirect')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 1234, app)
    server.serve_forever()

Which is running on http://blogologue.com:1234 now. Give it a try, in total I think I spent 1 hour setting up Pyramid and getting this URL shortening service running. That is very interesting and promising, think I'll have to find some project to get more familiar with Pyramid.

[Update..] See in the comments section for an updated code example that works harder to get an actual URL.

[Permalink] [By morphex] [Python-only or > 0.5 Python related (Atom feed)] [2013 20 Jan 10:15 GMT+2]

Comments

Updated code example

By: Morten W. Petersen

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid import httpexceptions

global urls
urls = []

def redirect(request):
    url = urls[int(request.matchdict['index'])]
    if url:
        raise httpexceptions.HTTPFound(url)
    else:
        return Response("Empty URL..")

def index(request):
    try:
        referer = request.params['b']
        if not referer:
            referer = request.referer
        if not referer:
            return Response("Sorry, referer was empty")
        global urls
        urls.append(referer)
        return Response("""Shortened URL: http://blogologue.com:1234/r%s""" % (len(urls)-1))
    except KeyError:
        return Response("""<html><head><title>Redirect service</title></head>
    <body><h1>Redirect services</h1>
    <p>Hello there, welcome :)</p>

    <p>To get started using this URL shorting service,
    please drag the following link to your bookmark
    bar, and click on the link on whatever page you'd
    like bookmarked and accessible via this URL shortening
    service.</p>

    <p><a href="javascript:document.location='http://blogologue.com:1234/?b='+document.location"
         >Shorten URL</a></p>
    </body>
    </html>""")

if __name__ == '__main__':
    config = Configurator()
    config.add_route('index', '/')
    config.add_view(index, route_name='index')
    config.add_route('redirect', '/r{index}')
    config.add_view(redirect, route_name='redirect')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 1234, app)
    server.serve_forever()


Add comment (text format)

Passphrase

A passphrase is required to comment on this weblog. It is required to make sure that bots aren't doing automatic spamming. It is: nit is the best!.

Title

Name

Email

Comment