Oracle Developers
Published in

Oracle Developers

Create a Simple Python Web Server on OCI

By Chris Bensen

Photo by Jan Kopřiva from Pexels

If you prefer you can read this blog post on GitHub here.

This article may seem obvious to some but others need to know how to get started. Having a server running in the cloud 24–7 is great but chances are you actually want to do something with it. That’s where the steps in this article come in.

Prerequisites

  1. You have an OCI account or a Free Tier Account.
  2. You have created a Compute Instance.
  3. You have locked down ssh on your compute to only your computer.

Find out more about Compute and other Oracle Cloud documentation here. For interactive support and community check out Oracle’s public Slack channel for developers.

Build a Web Server

Now, let’s create the web server. We will live dangerously and open a port so anyone can access the server on port 80. This should be fine as everything else is locked down but this isn’t how you’d want to run in a production environment. Then we will create a simple HTML file and a simple Python script to act as a terrible web server. I say terrible because there is no way you would want to run this in production. This is just an example and test. But it is a great place to start.

1. Click the link next to “Subnet” field, this will bring you to the list of Virtual Cloud Networks:

2. Click the VCN:

3. Click on the “Default Security List”:

4. Under “Ingress Rules” you can see SSH (port 22). Click “Add Ingress Rules”:

5. On the “Add Ingress Rules” dialog, set source CIDR “0.0.0.0/0” and set destination port range to “80”:

6. The rules now look like this:

7. Open up the VM’s firewall. So SSH into your VM:

ssh -i ~/.ssh/oci_id_rsa opc@${INSTANCE_IP}

8. Then run the following commands to open up port 80:

sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
sudo firewall-cmd --permanent --zone=public --add-port=80/udp
sudo firewall-cmd --reload

9. Create index.html:

<!DOCTYPE html>
<html>
<body>
Hello World
</body>
</html>

10. Create server.py:

#!/usr/bin/python3
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import json
from socketserver import ThreadingMixIn
import threading
hostName = “0.0.0.0”
serverPort = 80
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
# curl http://<ServerIP>/index.html
if self.path == “/”:
# Respond with the file contents.
self.send_response(200)
self.send_header(“Content-type”, “text/html”)
self.end_headers()
content = open(‘index.html’, ‘rb’).read()
self.wfile.write(content)
else:
self.send_response(404)
returnclass ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
“””Handle requests in a separate thread.”””
if __name__ == “__main__”:
webServer = ThreadedHTTPServer((hostName, serverPort), Handler)
print(“Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print(“Server stopped.”)

11. Run the web server:

sudo python3 server.py

12. Now from any computer use a web browser and access the url http://<ServerIP>

And viola! Enjoy your web server. Follow along because I’ve just been creating the building blocks. Next week I have some really fun things!

This requires an Oracle Cloud account. You can get one for free called the Free Tier Account.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chris Bensen

Chris Bensen

I make stuff for myself and Oracle. Creator of the Worlds Largest Raspberry Pi Cluster and Lego Computer.