How do I access the webpage that my Python script makes?

Question:

I am using MobaXterm to SSH into an EC2 instance. In the EC2 instance, I made a script that looks like the following:

`

#!/usr/bin/env python3

import mysql.connector

# Connect to the MariaDB database
host = "localhost"
username = "DBUser"
password = "DBUserPassword"
dbname = "IoTSchema"

conn = mysql.connector.connect(
    host=host,
    user=username,
    password=password,
    database=dbname
)

# Check for connection errors
if conn.is_connected():
  print("Connected to MariaDB database")
else:
  print("Connection failed")
  exit()

# Execute SQL queries to retrieve the desired data from the two tables
cursor = conn.cursor()

query1 = "SELECT * FROM iot_sensors"
cursor.execute(query1)
conn.close()

conn = mysql.connector.connect(
    host=host,
    user=username,
    password=password,
    database=dbname
)

cursor = conn.cursor()

query2 = "SELECT * FROM appliances"
cursor.execute(query2)

# Generate the HTML code for the first table
html = "<table>"
html += "<tr><th>Column 1</th><th>Column 2</th></tr>"

for row in cursor:
  html += "<tr>"
  html += "<td>" + row[0] + "</td>"
  html += "<td>" + row[1] + "</td>"
  html += "</tr>"

html += "</table>"

# Generate the HTML code for the second table
html += "<table>"
html += "<tr><th>Column 1</th><th>Column 2</th></tr>"

for row in cursor:
  html += "<tr>"
  html += "<td>" + row[0] + "</td>"
  html += "<td>" + row[1] + "</td>"
  html += "</tr>"

html += "</table>"

# Print the HTML code
print(html)

# Close the database connection
conn.close()

`

The aim of the script is to extract the tables from my MariaDB database and print it into 2 separate tables. When I execute the script, the following is returned.
Result of running the script

I am not sure if the output is correct, but what I expected was to be able to access the webpage on my local machine using the EC2 instance’s public IP address, but the connection was refused.

I have ensured that my instances allows all ip protocols, not sure what else I can try to fix this. Any help is appreciated, thank you.

Asked By: emil03

||

Answers:

Your script just prints the HTML into the console output. To serve it as a webpage that can be accessed over the internet, you need to do a few more things:

  1. Save the html page onto disk
  2. Serve the page using a web server like Apache
  3. Open the port your web server is running on your instance’s security group

This should enable you to access the page via your instance’s public IP. However if you just want to view the single html page, there’s an easy way to serve it with python3 python -m http.server without having to set up and run a separate web server.

Alternatively since you’re already running this on AWS, you could save the html file to an S3 bucket and access it from there.

Answered By: chamal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.