Category Archives: Hello World

Python, Bottle and websockets

Here is a simple websockets demo, using Python and Bottle, based on the example on the Bottle website

  1. Requirements:
    • pip install bottle
    • pip install python-dev
    • pip install gevent-websocket
  2. Create websockets.py, from the source code at http://bottlepy.org/docs/dev/async.html#finally-websockets
  3. python websockets.py
    • This starts the (websockets) server. Note that it doesn’t show any output
  4. Create websockets.html, again from http://bottlepy.org/docs/dev/async.html#finally-websockets
    • Make sure to change the websocket address (from “ws://example.com:8080/websocket”). On your local machine this should be “ws://localhost:8080/websocket”
  5. Load websockets.html in your browser (e.g. as a local file, at file:/// etc)
    • This should come up with an alert saying: Your message was ‘Hello, world’

Django – Hello World

Pre-requisites:

  1. easy_install, pip, virtualenv – getting ready for Django
  2. Installing Django

Hello World project in <dev root>/Projects/HelloWorldDjango

  1. Switch to <dev root>/Projects
  2. django-admin.py startproject HelloWordDjango (create the basic project files)
  3. cd HelloWorldDjango
  4. Check if it works so far: python manage.py runserver (start the test server)
  5. Ctrl-C to stop the test server
  6. Create the view: In <dev root>/Projects/HelloWorldDjango/HelloWorldDjango, create a file called “views.py” with the following content:
    from django.http import HttpResponse

    def hello(request):
    return HttpResponse(“Hello world”)

  7. Set the URL:
    1. In <dev root>/Projects/HelloWorldDjango/HelloWorldDjango/urls.py, add the following to the import line:
      from HelloWorldDjango.views import hello
    2. In the same file, add the following to the urlpatters list:
      (‘^hello$’, hello),
  8. Start the test server: in <dev root>/Projects/HelloWorldDjango, python manage.py runserver
  9. In your browser go to http://localhost:8000/hello. This should show you “Hello world”