How to Install Bottle on the Raspberry Pi

You will need easy_install to install bottle. In order to install easy_install, you will need Distribute:

curl -O https://bootstrap.pypa.io/ez_setup.py  
sudo python ez_setup.py  
sudo easy_install bottle

Now let's test it out. Open up nano or another text editor:

sudo nano hello_world.py

and copy and past the hello word script from http://bottlepy.org/

<span class="kn">from</span> <span class="nn">bottle</span> <span class="kn">import</span> <span class="n">route</span><span class="p">,</span> <span class="n">run</span><span class="p">,</span> <span class="n">template</span>

<span class="nd">@route</span><span class="p">(</span><span class="s">'/hello/&lt;name&gt;'</span><span class="p">)</span>  
<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">'World'</span><span class="p">):</span>  
    <span class="k">return</span> <span class="n">template</span><span class="p">(</span><span class="s">'&lt;b&gt;Hello {{name}}&lt;/b&gt;!'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>

<span class="n">run</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s">'localhost'</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">8080</span><span class="p">)</span>

Let's execute the script:

python hello_world.py

On my regular compute I went to http://192.168.1.101/hello/matthew but was unable to load it (I received the IP address from ifconfig)

On my RPi I executed the following:

curl http://localhost:8080/hello/matthew

and received the expected result: <b>Hello matthew</b>!. After looking around, I learned that the host cannot be kept to 'localhost', but should instead be set to '0.0.0.0'. Change the hello world script to:

from bottle import route, run, template

@route('/hello/<name>')  
def index(name='World'):  
    return template('<b>Hello {{name}}</b>!', name=name)

debug(True)  
run(host='0.0.0.0', port=8080, reloader=True)

And it works.

Update: I've had problems with Bottle turning itself off after some period of time. Google suggested installing the meinheld server via:

sudo apt-get <em>install</em> python-<em>pip</em>  
sudo pip install meinheld

and then instruct Bottle to use the server in the run() function:

 run(server="meinheld", host='0.0.0.0', port=8080, reloader=True)

Comments

Add Comment

Name

Email

Comment

Are you human? - two = 0