Archive

Getting started with Python on IBM Bluemix

Share this post:

Taking inspiration from Rob Phippen’s blog post here on Thoughts on Cloud titled “Getting started with IBM Bluemix and Node.js,” I would like to guide you through the deployment of a basic web server written in Python. Yes, Python.

IBM Bluemix does support programming languages other than those advertised on the dashboard, such as Java, Node.js, Ruby on Rails and Ruby Sinatra. Bluemix also allows you to bring your own buildpack or use a buildpack prepared by the Cloud Foundry community, such as .Net, PHP and Python. However, be aware that not all Cloud Foundry buildpacks are compatible with Bluemix. For Python, I recommend you stick with the one used in this blog post.

Here we go!

Step 1: Log in to IBM Bluemix.

1. Navigate to your browser.
2. Go to https://www.bluemix.net.
3. Click on LOG IN and authenticate using your IBM ID credentials.

If you do not have an IBM ID already, sign up here to get one.  If you do not have an IBM Bluemix account, you can sign up for a complimentary trial on the IBM Bluemix welcome screen.

Python on Bluemix 1

Step 2: Install the Cloud Foundry command-line interface.

1. Go to https://github.com/cloudfoundry/cli.
2. Scroll down to the Downloads section of the README.md file, and then select and download the installer for your operating system.

Python on Bluemix 2
3. Install the Cloud Foundry (cf) command-line interface.
4. Open a command line on your operating system and use the cf -v command to verify that cf is working.

$ cf -v
cf version 6.2.0-c9d4aaa-2014-06-19T22:04:01+00:00

Step 3: Write the code.

1. Create a file named “server.py” containing the basic web server code that serves files relative to the current directory.

import os
import http.server
import socketserver

PORT = int(os.getenv('VCAP_APP_PORT', '8000'))

Handler = http.server.SimpleHTTPRequestHandler

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()

This code is based on sample code  provided in the Python documentation. A few modifications are required for it to work in the Cloud Foundry framework. These are:

• In the original sample code, the PORT variable is hardcoded: PORT = 8000. This has been changed to a variable being imported from an environment variable named  VCAP_APP_PORT.

• The line import os has been added to support retrieval of the VCAP_APP_PORT environment variable.

2. Create an empty file named requirements.txt. The file should be empty because this application does not have any dependencies that need to be installed.
3. Create a file named runtime.txt containing the Python version you wish to use as runtime.
python-3.4.1

Step 4: Deploy the sample application.

1. Open a command line on your operating system and navigate to the directory containing your application.
2. Log in to IBM Bluemix using the commands cf api https://api.ng.bluemix.net and cf login.

$ cf api https://api.ng.bluemix.net
Setting api endpoint to https://api.ng.bluemix.net...
OK

API endpoint:   https://api.ng.bluemix.net (API version: 2.2.0)
User:           renesv@dk.ibm.com
Org:            renesv@dk.ibm.com
Space:          dev

$ cf login
API endpoint: https://api.ng.bluemix.net

Email> renesv@dk.ibm.com

Password>
Authenticating...
OK

Targeted org renesv@dk.ibm.com

Targeted space dev


API endpoint:   https://api.ng.bluemix.net (API version: 2.2.0)
User:           renesv@dk.ibm.com
Org:            renesv@dk.ibm.com
Space:          dev

3. Push the sample application to the Bluemix runtime environment using the command cf push myserver1 -m 32M -b https://github.com/cloudfoundry/cf-buildpack-python.git -c "python server.py".

$ cf push myserver1 -m 32M -b https://github.com/cloudfoundry/cf-buildpack-python.git -c "python server.py"
Creating app myserver1 in org renesv@dk.ibm.com / space dev as renesv@dk.ibm.com...
OK

Using route myserver1.mybluemix.net
Binding myserver1.mybluemix.net to myserver1...
OK

Uploading myserver1...
Uploading app files from: /home/renesv/cf
Uploading 647, 3 files
OK

Starting app myserver1 in org renesv@dk.ibm.com / space dev as renesv@dk.ibm.com...
OK
-----> Downloaded app package (3.7M)
    Cloning into '/tmp/buildpacks/cf-buildpack-python'...
    Submodule 'buildpack-packager' (https://github.com/cf-buildpacks/buildpack-packager.git) registered for path 'buildpack-packager'
    Submodule path 'buildpack-packager': checked out '9af0068885b4dc76d3ca09d9d334766d539fb9e8'
    Cloning into 'compile-extensions'...
    Submodule path 'compile-extensions': checked out 'e58222a8f87aaafaf9a52a06b59755e518ad993e'
-----> Preparing Python runtime (python-3.4.1)
-----> Installing Setuptools (3.6)
-----> Installing Pip (1.5.6)
-----> Installing dependencies using Pip (1.5.6)
       You must give at least one requirement to install (see "pip help install")
-----> Uploading droplet (37M)

0 of 1 instances running, 1 starting
1 of 1 instances running
App started

Showing health and status for app myserver1 in org renesv@dk.ibm.com / space dev as renesv@dk.ibm.com...
OK

requested state: started
instances: 1/1
usage: 32M x 1 instances
urls: myserver1.mybluemix.net

     state     since                    cpu    memory       disk
#0   running   2014-07-23 11:15:03 PM   0.0%   32M of 32M   137M of 1G

Step 5: Verify that your application is running.

1. Navigate to your web browser and locate the IBM Bluemix welcome screen.
2. On the IBM Bluemix dashboard, you should see your myserver1 application running.

Python on Bluemix 3

3. Go to http://myserver1.mybluemix.net to access the sample application.

Python on Bluemix 4

Please use the comments section below to share your experiences of using buildpacks in the Bluemix environment.

More stories

Why we added new map tools to Netcool

I had the opportunity to visit a number of telecommunications clients using IBM Netcool over the last year. We frequently discussed the benefits of have a geographically mapped view of topology. Not just because it was nice “eye candy” in the Network Operations Center (NOC), but because it gives an important geographically-based view of network […]

Continue reading

How to streamline continuous delivery through better auditing

IT managers, does this sound familiar? Just when everything is running smoothly, you encounter the release management process in place for upgrading business applications in the production environment. You get an error notification in one of the workflows running the release management process. It can be especially frustrating when the error is coming from the […]

Continue reading

Want to see the latest from WebSphere Liberty? Join our webcast

We just released the latest release of WebSphere Liberty, 16.0.0.4. It includes many new enhancements to its security, database management and overall performance. Interested in what’s new? Join our webcast on January 11, 2017. Why? Read on. I used to take time to reflect on the year behind me as the calendar year closed out, […]

Continue reading