Rest API with Flask & SQL Alchemy on GoDaddy using MySQL as the database. (pt 2)

We ended our last session with the Python app all setup on GoDaddy. Now we will continue our work and setup the python environment with all of the packages that we need to run our Flask-API app. To enter the python environment that was created you need to go the Web Application page that was created and grab the text that is presented.

Getting into the Python Environment on GoDaddy

There are two ways of getting to the Terminal in GoDaddy. You can open up a terminal on your local machine or you can open one up through the cPanel Main Page as shown below.

Access Terminal from cPanel

When your terminal opens you can paste the text from the application page into it and run the command.

This will put you in the right Environment and Directory to do a Pip install of the packages needed. Below is a screen shot of all the packages that need to be installed.

pip freeze on the environment

Here is an easy list of the packages to install.

pip install flask
pip install sqlalchemy
pip install flask_sqlalchemy
pip install pymysql
pip install marshmallow
pip install flask-marshmallow
pip install marshmallow-sqlalchemy

Once those are installed you should get a listing of everything install like above. From here you will need to add your code to the app.py file in your application directory. You can copy and paste the code from pt into your app.py file and then make these changes to it.

import pymysql

Add an import for pymysql

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://username:password@localhost/dbname'

You should notice the addition of “+pymysql” here is different from the original code.

# Init schema
product_schema = ProductSchema()
products_schema = ProductSchema(many=True)

The above is a change also. I had to remove the strict=true from the product_schema and the products_schema

The last thing I need to do was create the database. The first time I ran the code it created the tables and populated the data with the json that I fed it through Postman.

I hope this helps you get started with Python on GoDaddy. If you have any questions drop them in the comments.

Rest API with Flask & SQL Alchemy on Godaddy using MySQL as the database.

I have a Godaddy website and decide I wanted to try creating a Flask API connected to a MySQL database. My first order of business was watching this video from Traversy Media REST API With Flask & SQL Alchemy. You can find the code here at Github.

With that I had to setup a Python Application on Godaddy. In order to do this you need to go to your Godaddy cPanel interface. Before you setup the application you will need to create a place to hold the application. I created a sub domain to do this. I called it app.scriptkrewe.info.

You will need this information when you setup your Python app as we will see. Once this is done you can head back to the main cPanel screen and go the the Software section. Click on the Setup Python App link.

Godaddy cPanel Software Section

This will bring you to the Python Web Applications page.

This this page you can view the status of your web applications, stop them, start them, delete them or edit them. We will create one using the Create Application button. Below is what the screen looks like.

Python App Screen

This is where the magic happens. You will need to make some decisions here. The first of which is the version of Python your app needs. Second will be the Application Root. This directory will be created for you with all the files you need in your Home directory. So it will be at the home/username/appdirectory level. After that you will need to pick a site to host your application. This Should be an Empty directory. If you have an index.html or some other file it will pick that up first. Next is your applications start up file. By default Godaddy creates this file in the directory that you named as your Application root and names it app.py. You can change this to what ever you want to work with your application but you will need to modify the passenger_wsgi.py file that is created for you. You will need to identify your app entry point. Usually this is app for app = Flask(__name__). Finally it is a good thing to create a log file. This will be extremely helpful in the log run. I named my file after the application root name.

From here you can Save and Start the app. This will allow you to go to the url of your site and see if python is working.

In my next post we will start the process of putting together the building blocks of the application. See you then.

xMatters to MS Teams alert using Python.

Here is some code I used to create an alert to send the number of people in xMatters to MS Teams so I could track the number of people created. We have an automated task that creates user and I need to track it to make sure we don’t have any spikes. I created the xMatters call using Postman and then selected the code option and chose Python. This is great for getting it working and also hashing the authorization data.

#!/usr/bin/env python3

import requests
import json
url = "https://yourAccount.xmatters.com/api/xm/1/people/"

payload = {}
headers = {
    'Authorization': 'Basic your authorization from Xmatters'
}

response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
print("Count: " + str(data["count"]))
print("Total: " + str(data["total"]))

teamsURL = "Your teams web hook url"

# "Count": str(data["count"]),
# "Total": str(data["total"])

teamsPayload = {
    "text": "Count:" + str(data["count"]) + '  ' + "Total:" + str(data["total"])

}
headers = {
    'Content-Type': 'application/json'
}

teamsResponse = requests.post(teamsURL, headers=headers,
                              data=json.dumps(teamsPayload))
//print(teamsResponse.text.encode('utf8'))

Head phone stand

Are you tired of your headphones being in the way. Try this stand out. I got it at the container store!

This is awesome!

Another Certificate! Woo Hoo

Doing an join on two different indexes in Splunk

I know this in probably covered somewhere in some document or blog post or support site somewhere…….but I couldn’t find it. At least not a down and dirty simple way to start so here is my way to do it. Thanks to Maggie Kostiew for giving me the “search” parameter. The hard part was the “[ search” part. That was shown no where. So I hope this help some new guy like me somewhere.

index="device_list"
| table serialNumber, manufacturer, modelName, hardwareVersion, softwareVersion, wanAccessType, macAddress, pppUsername
| join inner serialNumber
    [search index="device_op" error=* | table serialNumber, error]

Setting a field in json as your _time field in Splunk

A problem I have been encountering is trying to set a date field within my json response file as the _time field in my Splunk data upon import. I have read lots of posts on it and found that less is more when it comes to accomplishing this. Adding the following stanza in a props.conf file in the /opt/splunkforwarder/etc/system/local folder accomplished this. As you can see by the path is is on the Splunk Univeral Forwarder.

The json file is created by a python script file that hits an API and returns the data (more on that later). The date field is the second date field in the json string. So Splunk was grabbing the first one and sometimes it was taking the file creation date and time.

After trying several different variations of the stanza the below code finally worked. I hope this helps you!

[source::/data/logs/prod.json]
TIME_FORMAT=%Y-%m-%dT%H:%M:%S.%3N
TIME_PREFIX="lastInformTime": "
MAX_TIMESTAMP_LOOKAHEAD = 24
MAX_DAYS_HENCE = 10

Here are some of the other answers I tried.

Looking for a standup desk for your home office

I recently decide to purchase a standup desk for my home office. I researched a couple of different brands but I decided to go with a Varidesk from Office Depot due to availabilty. Other desks would have taken months to be delivered and Office Depot had mine to me in 3 day’s.

VARIDESK ProDesk Electric Height-Adjustable Desk, 48"W, Reclaimed Wood

I have had it now for three weeks and couldn’t be happier with it. Setup was a breeze. No fumbling with a dozen different screws and trying to hold the pieces together. Just 4 screws to secure the legs to the top after they have been slid into place (in fact I didn’t even put the screws in till yesterday). The brace slide onto the legs with a wedge to hold it in place. They supply a nice rubber mallet to tap the brace into place and you can use it for computer repair later 🙂 . It’s a very solid piece of furniture. You can have preprogramed heights so at the touch of a button you are standing or sitting.

Groups in Outlook

In order to access your Contact Groups you must click on the people icon in the left hand navigation pane.

To create a new group click on the New Contact Group. This will create the group in your local contacts.
You can search from an existing group by searching in the contacts.
This will bring up your group and you can double click on it to modify it.
From here you can add or remove people from your group.
Make sure you select From Outlook contacts to get your local contacts list.