10 Realtime Python Automation Scripts
10 Realtime Python Automation Scripts
DEVOPS SHACK
10 Real-Time Python Automation Scripts
Enrol To batch-7 | DevSecOps & Cloud DevOps
1
DevOps Shack By Aditya Jaiswal
#Multiple Recipent
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from
email.mime.base import MIMEBase
from email import encoders import
os
2
DevOps Shack By Aditya Jaiswal
3
DevOps Shack By Aditya Jaiswal
Script-2 | WEB-SCRAPING
pip install requests pip
install beautifulsoup4
import requests
from bs4 import BeautifulSoup
def scrape_headlines_demo(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract all headlines from the website (assuming they are in <h2> tags)
headlines = soup.find_all('h2')
This task involves moving files of a particular type (e.g., .txt files) from one folder to another.
import os
import shutil
def move_files_by_type(source_dir, dest_dir, file_extension):
for filename in os.listdir(source_dir):
if filename.endswith(file_extension):
shutil.move(os.path.join(source_dir, filename), os.path.join(dest_dir, filename))
# Example usage:
move_files_by_type('/path/to/source', '/path/to/destination', '.txt')
This will move all .txt files from the source directory to the destination directory.
4
DevOps Shack By Aditya Jaiswal
az aks install-cli
C:\Users\jaisw\.azure-kubectl
$env:Path += ";C:\Users\jaisw\.azure-kubectl"
import requests
import subprocess
def get_weather_data(city):
"""Fetches weather data for a specific city using WeatherAPI.""" api_key =
'91da448afecb4b83af642416241310' # Replace with your WeatherAPI key base_url
= f'http://api.weatherapi.com/v1/current.json?key={api_key}&q={city}'
response = requests.get(base_url)
data = response.json()
# If the weather condition is "Heavy rain", scale the number of pods in AKS if "Heavy
rain" in weather: print("Weather condition is heavy rain. Scaling the number of pods in
AKS...") scale_aks_pods_using_kubectl(namespace='blogging-app',
deployment_name='bankapp', replicas=2) # Set to 3 replicas else:
print("Weather condition is not heavy rain. No scaling action is required.")
else:
print(f"Error fetching data for {city}: {data['error']['message']}")
# Example usage:
5
DevOps Shack By Aditya Jaiswal
get_weather_data('London')
def basic_chatbot():
print("Welcome to Customer Support (type 'quit' to exit)")
chat = Chat(pairs, reflections)
chat.converse()
6
DevOps Shack By Aditya Jaiswal
if slack_response.status_code == 200:
print(f"Slack notification sent: {message}")
else:
7
DevOps Shack By Aditya Jaiswal
except requests.exceptions.RequestException as e:
print(f"Error while checking server health: {e}") # Optional:
Send failure notification to Slack error_message = f"Error
while checking server health: {e}"
requests.post(slack_webhook, json={'text': error_message})
8
DevOps Shack By Aditya Jaiswal
# Example usage:
cleanup_old_files(r'C:\Users\jaisw\Videos\Parameters', 30) # Deletes files older than 30 days
9
DevOps Shack By Aditya Jaiswal
# Example usage:
subscription_feed_url =
"https://www.youtube.com/feeds/videos.xml?channel_id=UCK96F7QJuTYgNM0FFT7DvJw"
output_folder = "BACKUP"
download_latest_videos(subscription_feed_url, output_folder)
10
DevOps Shack By Aditya Jaiswal
11
DevOps Shack By Aditya Jaiswal
12