Oracle by Example brandingDeploy a Python Application to Oracle Cloud

section 0Before You Begin

This 10-minute tutorial shows you how to deploy a Python application to Oracle Application Container Cloud Service.

Background

Oracle Application Container Cloud Service lets you deploy Java SE, Java EE, Node.js, PHP, Ruby, Go, .Net Core, and Python applications to the Oracle Cloud.

What Do You Need?

An Oracle Cloud account with access to Oracle Application Container Cloud Service


section 1Create a Python Application

  1. Create the python-app project directory in your local system.
  2. In the python-app directory, create the app.py file and add the following content:
    #!/usr/bin/python
    import os
    from http.server import BaseHTTPRequestHandler, HTTPServer
    PORT_NUMBER = int(os.environ.get('PORT', 8084))
    
    # HTTPRequestHandler class
    class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
     
      # GET
      def do_HEAD(self):
            # Send response status code
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            return
      # GET
      def do_GET(self):
            # Send response status code
            self.send_response(200)
     
            # Send headers
            self.send_header('Content-type','text/html')
            self.end_headers()
     
            # Send message back to client
            message = "Hello world!"
            # Write content as utf-8 data
            self.wfile.write(bytes(message, "utf8"))
            return
     
    def run():
      print('starting server...')
     
      # Server settings
      # Choose port 8080, for port 80, which is normally used for a http server, you need root access
      server_address = ('0.0.0.0', PORT_NUMBER)
      httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
      print('running server...')
      httpd.serve_forever()
      
    run()
                    

section 2Prepare the Application for Deployment

  1. In the python-app directory, create the manifest.json file and add the following content:
    {
      "runtime": {
        "majorVersion": "3.6.0"
      },
      "command": "python app.py",
      "notes": "Simple REST Service"
    } 
  2. Open a command-line (or Terminal in Linux), go to the python-app directory, and compress the manifest.json file and the app.py file together in a .zip file.
    zip python-app.zip app.py manifest.json 

section 3Deploy Your Application to Oracle Application Container Cloud Service

  1. Open the Oracle Application Container Cloud Service console.
  2. In the Applications list view, click Create Application and select Python.
  3. In the Application section, enter a name for your application and click Browse.
  4. On the File Upload dialog box, select the python-app.zip file you created in the previous section and click Open.
  5. Keep the default values in the Instances and Memory fields and click Create.
  6. Wait until the application is created. The URL is enabled when the creation is completed.
  7. Click the URL of your application.
    HelloWorldApp response
    Description of the illustration python-app-response.png

more informationWant to Learn More?