How to get the IP Address in Python

If you need to get the IP Address in your Python application, then you can do the following: Option 1 – Using socket.gethostname() 1 2 import socket print(socket.gethostbyname(socket.gethostname())) Option 2 – Using socket.getsockname() 1 2 3 4 import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) print(s.getsockname()[0]) Option 3 – Using the netifaces module 1 2 3 4 from netifaces import interfaces, ifaddresses, AF_INET for ifaceName in interfaces(): addresses = [i['addr'] for i in ifaddresses(ifaceName).

How to use SSH in your Python application

If you need to make an SSH connection and issues commands over SSH using your Python application, then you can do the following: Option 1 – Using the paramiko library 1 2 3 ssh = paramiko.SSHClient() ssh.connect(server, username=username, password=password) ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute) Option 2 – Using the subprocess module 1 subprocess.check_output(['ssh', 'my_server', 'echo /*/']) Option 3 – Using the subprocess module 1 subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd='ls -l'), shell=True, stdout=subprocess.

How to Pause a Program in Python

If you need to pause the execution of your Python program, then you can do the following: Option 1 – Using time.sleep() 1 2 3 4 import time time_duration = 3.5 time.sleep(time_duration) Option 2 – Using input() 1 2 name = input("Please enter your name: ") print("Name:", name) Option 3 – Using os.system("pause") 1 2 3 import os os.system("pause")

How to Convert String to Double in Python

If you need to convert a String to a Double in your Python code: Option 1 – Convert String to Double using float() 1 2 3 string = '1234.5678' myfloat = float(string) print(myfloat) Option 2 – Convert String to Double using decimal.Decimal() 1 2 3 4 5 from decimal import Decimal string = '1234.5678' myfloat = Decimal(string) print(myfloat)

How to a Run Bash Command in Python

If you need to run a bash command in your Python code, then you can do the following: Option 1 – Using run() from subprocess Module 1 2 3 4 from subprocess import PIPE comp_process = subprocess.run("ls",stdout=PIPE, stderr=PIPE) print(comp_process.stdout) Option 2 – Using Popen() from subprocess Module 1 2 3 4 5 6 from subprocess import PIPE process = subprocess.Popen("ls",stdout=PIPE, stderr=PIPE) output, error = process.communicate() print(output) process.kill

How to Force Redeployment of AWS API Gateway using AWS CloudFormation

If you have an AWS API Gateway resource, and need it to force a redeployment using CloudFormation, then you can use the TIMESTAMP trick. Example AWS CloudFormation Extract template.yaml extract: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 APIGatewayStage: Type: AWS::ApiGateway::Stage Properties: StageName: !Sub ${EnvironmentTagName} RestApiId: !Ref APIGateway DeploymentId: !Ref APIGatewayDeployment__TIMESTAMP__ TracingEnabled: true MethodSettings: - DataTraceEnabled: true HttpMethod: "*" LoggingLevel: INFO ResourcePath: "/*" MetricsEnabled: true APIGatewayDeployment__TIMESTAMP__: Type: AWS::ApiGateway::Deployment Properties: RestApiId: !

How to Deploy React App to S3 and CloudFront

If you would like to deploy a React App to AWS S3 and AWS CloudFront, then you can follow this guide. The following solution creates a React App and deploys it to S3 and CloudFront using the client’s CLI. It also chains commands so that a React build, S3 sync and CloudFront invalidation can occur with a single command. Code available at GitHub https://github.com/ao/deploy-react-to-s3-cloudfront Target Architecture Guided Deployment Solution Create a directory for the application:

How to Read a File in Python

If you need to read a file in Python, then you can use the open() built-in function to help you. Let’s say that you have a file called somefile.txt with the following contents: 1 2 Hello, this is a test file With some contents How to Open a File and Read it in Python We can read the contents of this file as follows: 1 2 f = open("somefile.txt", "r") print(f.

How to Drop Columns in Pandas Only If Exists

If you have a Pandas DataFrame, and want to only drop columns if they exist, then you can do the following: Add parameter errors to DataFrame.drop: errors : {‘ignore’, ‘raise’}, default ‘raise’ If ‘ignore’, suppress error and only existing labels are dropped. 1 df = df.drop(['row_num','start_date','end_date','symbol'], axis=1, errors='ignore') An example of how to Ignore Errors with .drop() 1 2 3 4 5 6 df = pd.DataFrame({'row_num':[1,2], 'w':[3,4]}) df = df.drop(['row_num','start_date','end_date','symbol'], axis=1, errors='ignore') print (df) w 0 3 1 4

[Solved] An error occurred while calling o86.getDynamicFrame. Exception thrown in awaitResult:

If you are running a GlueJob in AWS and get the following error: An error occurred while calling o86.getDynamicFrame. Exception thrown in awaitResult: Then you need to view the CloudWatch logs to help you pinpoint where the problem is occuring. How to solve the Exception thrown in awaitResult It’s highly likely that the issue is in an expired IAM Role. When a Role is created in IAM, the default maximum session duration is set to 1 hour.

AWS CDK Commands

The AWS Cloud Development Kit (CDK) comes with a list of commands that you need to know: cdk list (ls) Lists the stacks in the app cdk synthesize (synth) Synthesizes and prints the CloudFormation template for the specified stack(s) cdk bootstrap Deploys the CDK Toolkit staging stack; see Bootstrapping cdk deploy Deploys the specified stack(s) cdk destroy Destroys the specified stack(s) cdk diff Compares the specified stack with the deployed stack or a local CloudFormation template cdk metadata Displays metadata about the specified stack cdk init Creates a new CDK project in the current directory from a specified template cdk context Manages cached context values cdk docs (doc) Opens the CDK API reference in your browser cdk doctor Checks your CDK project for potential problems You can learn more about the CDK here: https://docs.

How to Make a Java Jar File Executable

Let’s say you have a Java project as follows: 1 2 3 4 5 6 package ms.ao.something; public class MyProject { public static void main(String...args) throws Exception { System.out.println("Hello world!"); } } Now you want to build this and make it a self contained executable Jar. If you do a mvn clean install or mvn clean package, and try and run it as follows: 1 java -jar target/my-java-1.0-SNAPSHOT.jar You will get the following error:

How to List All Resources in an AWS Account

If you need to see a list of all the resources in your AWS Account, then you need to look into the Tag Editor. Step 1 – Tag Editor Search for Tag Editor in the navigation search at the top of the AWS Console. Select the Resource Groups & Tag Editor. Step 2 – Find Resources From the left hand menu, select Tag Editor Step 3 – Filter your Search Requirements From the Regions drop down, select All regions and then select All supported resource types from the Resource types drop down.

Fixed size left column and fluid right column both with 100% height in CSS

If you need two (2) columns and want the left column to be a fixed size, but the right column to automatically take the remaining size of the window, then you can use the following solution. Follow the steps below, which include some CSS and some HTML. The CSS for our solution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 html, body { height: 100%; width: 100%; padding: 0; margin: 0; } .

How to Check if a Volume is Mounted in Bash

If you need to check if a volume is mounted in a Bash script, then you can do the following. How to Check Mounted Volumes First we need to determine the command that will be able to check. This can be done with the /proc/mounts path. How to Check if a Volume is Mounted in Bash 1 2 3 4 5 if grep -qs '/mnt/foo ' /proc/mounts; then echo "It's mounted.

How to Determine if a Bash Variable is Empty

If you need to check if a bash variable is empty, or unset, then you can use the following code: 1 if [ -z "${VAR}" ]; The above code will check if a variable called VAR is set, or empty. What does this mean? Unset means that the variable has not been set. Empty means that the variable is set with an empty value of "". What is the inverse of -z?

How to Order by File Size using the du command in Linux

If you use the du command to list all the file sizes on Linux: 1 2 3 du # or du -h # Human readable Then you would have noticed that they are not ordered by file size. Instead you can pass that result to the sort command as follows: 1 du -h | sort -h

How to Join Multiple MySQL Tables in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. Presenting the data let’s take two (2) tables as a demonstration for the code below. Users – Table 1 1 2 3 4 5 { id: 1, name: 'Carl', fav: 254}, { id: 2, name: 'Emma', fav: 254}, { id: 3, name: 'John', fav: 255}, { id: 4, name: 'Hayley', fav:}, { id: 5, name: 'Andrew', fav:} Products – Table 2