August 18, 2014 | Written by: Rene D. Svendsen
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.
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.
Step 2: Install the Cloud Foundry command-line interface.
1. Go to.
2. Scroll down to the Downloads section of the README.md file, and then select and download the installer for your operating system.
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 and cf login.
$ cf api Setting api endpoint to... OK API endpoint: (API version: 2.2.0) User: renesv@dk.ibm.com Org: renesv@dk.ibm.com Space: dev $ cf login API endpoint: Email> renesv@dk.ibm.com Password> Authenticating... OK Targeted org renesv@dk.ibm.com Targeted space dev API endpoint: (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 -c "python server.py".
$ cf push myserver1 -m 32M -b -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' () 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.
3. Go to to access the sample application.
Please use the comments section below to share your experiences of using buildpacks in the Bluemix environment., […]