From 0e3524d49e5a3aeeab95fb2da79e8946d215f5db Mon Sep 17 00:00:00 2001 From: Rishab Kumar <45825464+rishabkumar7@users.noreply.github.com> Date: Sun, 19 Feb 2023 22:50:23 -0500 Subject: [PATCH 1/5] Added Day 46 for Python --- 2023/day46.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/2023/day46.md b/2023/day46.md index e69de29..8b0c6bc 100644 --- a/2023/day46.md +++ b/2023/day46.md @@ -0,0 +1,47 @@ +# Day 46 - Web development in Python + +Python is quite capable when it comes to web development, and there are a variety of frameworks and modules are available for it. These can be used to create web applications. +Flask, Django, and Pyramid are a few well-known frameworks. The choice of framework will rely on the project's requirements. Each of these frameworks has advantages and disadvantages of its own. + +## Creating a basic web app using Flask: + +Creating a basic web application using Flask: Flask is a micro web framework for Python that is easy to learn and use. It provides a simple way to create web applications and APIs using Python. Here are some examples of Flask code for creating a basic web application: + +``` python +from flask import Flask + +app = Flask(__name__) + +@app.route('/') +def hello_world(): + return 'Hello, World!' +``` +This code creates a Flask application and defines a single route for the root URL (/). When the user visits the URL, the hello_world() function is called and returns the string 'Hello, World!'. + +## Working with databases: + +The majority of online applications need some sort of permanent storage, and Python offers a number of modules and frameworks for doing so. Popular options include Django ORM, Peewee, and SQLAlchemy. Here is an illustration of how to work with a SQLite database using SQLAlchemy: + +``` python +from flask import Flask +from flask_sqlalchemy import SQLAlchemy + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' +db = SQLAlchemy(app) + +class User(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(50)) + +@app.route('/') +def index(): + users = User.query.all() + return render_template('index.html', users=users) +``` +This code creates a SQLite database and a User table using SQLAlchemy. The index() function queries the database for all users and passes them to the template for rendering. + +Having a good understanding of how these web apps work, will help you with automation and deployment when it comes to practicing DevOps. +You can dive deeper into how you can build APIs using Python and serverless technologies like AWS Lambda, Azure Functions etc. + +I have a demo on [how I built a serverless resume API](https://github.com/rishabkumar7/AzureResumeAPI). From f513dbf883782c3139b5c0014f7bcc86972756f2 Mon Sep 17 00:00:00 2001 From: Rishab Kumar <45825464+rishabkumar7@users.noreply.github.com> Date: Sun, 19 Feb 2023 23:28:01 -0500 Subject: [PATCH 2/5] Added some contents to Day 47 --- 2023/day47.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/2023/day47.md b/2023/day47.md index e69de29..2cf9725 100644 --- a/2023/day47.md +++ b/2023/day47.md @@ -0,0 +1,31 @@ +# Day 47 - Automation with Python + +Using Python for infrastructure management involves automating the management of IT infrastructure, such as servers, databases, and networking equipment. This can include tasks like provisioning, configuration, and orchestration. +Python is a popular language for infrastructure management, and there are several tools and libraries available to help with this. Some popular tools for infrastructure management that use Python include: + +- Fabric +- PyWinRM +- Pulumi + +## Fabric + +Fabric is a Python library that can be used for streamlining SSH commands and remote execution of scripts, which can be used to automate server configuration and deployment +Here is an example in which we will be using the Fabric library to connect to a remote server using SSH, and then run the `ls -l` command on the remote server. The output of the command will be printed to the console. + +``` python +from fabric import Connection + +# Connect to the remote server +c = Connection('user@remote.host') + +# Run a command on the remote server +result = c.run('ls -l') + +# Print the output of the command +print(result.stdout) +``` + + +## PyWinRM + + A Python library that can be used to automate Windows Remote Management tasks, which can be used to automate Windows server configuration and management. From 2cdd2124c9377b4678e6e52664ccd51960edb4ea Mon Sep 17 00:00:00 2001 From: Rishab Kumar Date: Mon, 20 Feb 2023 17:17:56 -0500 Subject: [PATCH 3/5] Added Day 46 & 47 --- 2023.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2023.md b/2023.md index e6f52b9..fd5b4ad 100644 --- a/2023.md +++ b/2023.md @@ -100,8 +100,8 @@ Or contact us via Twitter, my handle is [@MichaelCade1](https://twitter.com/Mich - [] 🐍 43 > [Python Loops, functions, modules and libraries](2023/day43.md) - [] 🐍 44 > [Data Structures and OOP in Python](2023/day44.md) - [] 🐍 45 > [Debugging, testing and Regular expression](2023/day45.md) -- [] 🐍 46 > [](2023/day46.md) -- [] 🐍 47 > [](2023/day47.md) +- [] 🐍 46 > [Web development in Python](2023/day46.md) +- [] 🐍 47 > [Automation with Python](2023/day47.md) - [] 🐍 48 > [](2023/day48.md) ### AWS From c736642cbdef3c70ae37851c06d7910af54a574c Mon Sep 17 00:00:00 2001 From: Rishab Kumar Date: Mon, 20 Feb 2023 17:18:04 -0500 Subject: [PATCH 4/5] Update Day 47 content --- 2023/day47.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/2023/day47.md b/2023/day47.md index 2cf9725..28c2ec2 100644 --- a/2023/day47.md +++ b/2023/day47.md @@ -9,7 +9,7 @@ Python is a popular language for infrastructure management, and there are severa ## Fabric -Fabric is a Python library that can be used for streamlining SSH commands and remote execution of scripts, which can be used to automate server configuration and deployment +Fabric is a Python library that can be used for streamlining SSH commands and remote execution of scripts, which can be used to automate server configuration and deployment. Here is an example in which we will be using the Fabric library to connect to a remote server using SSH, and then run the `ls -l` command on the remote server. The output of the command will be printed to the console. ``` python @@ -29,3 +29,34 @@ print(result.stdout) ## PyWinRM A Python library that can be used to automate Windows Remote Management tasks, which can be used to automate Windows server configuration and management. + +## Pulumi + +Pulumi is a cloud infrastructure as code (CIaC) tool that lets you define and manage cloud resources in a variety of programming languages, including Python. + +You can use Pulumi to write Python code to describe your infrastructure as code, and then use the Pulumi CLI to deploy and manage it. Here is an example: + +``` python +import pulumi +from pulumi_aws import ec2 + +# Define an EC2 instance +server = ec2.Instance('server', + instance_type='t2.micro', + ami='ami-0c55b159cbfafe1', + tags={ + 'Name': 'cloud-server', + }, +) + +# Export the server's IP address +pulumi.export('ip_address', server.public_ip) +``` + +In this example, we're using the Pulumi Python SDK to define an EC2 instance on AWS. We specify the instance type, the AMI ID, and some tags for the instance, and then export the instance's public IP address. ou can then use the Pulumi CLI to deploy this infrastructure, which will create the EC2 instance on AWS. You can also use the Pulumi CLI to manage your infrastructure over time, making changes and updates as needed. + +## Resources: + +- [Learn more about Fabric]() +- [PyWinRM](https://github.com/diyan/pywinrm) +- [Pulumi - IaC Tool](https://www.pulumi.com/docs/reference/pkg/python/pulumi/) \ No newline at end of file From 3d67c82c13f1aa42016f0da10427f5200c21948e Mon Sep 17 00:00:00 2001 From: Rishab Kumar Date: Mon, 20 Feb 2023 17:19:27 -0500 Subject: [PATCH 5/5] Added link to Fabric docs --- 2023/day47.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2023/day47.md b/2023/day47.md index 28c2ec2..ad354e4 100644 --- a/2023/day47.md +++ b/2023/day47.md @@ -57,6 +57,6 @@ In this example, we're using the Pulumi Python SDK to define an EC2 instance on ## Resources: -- [Learn more about Fabric]() +- [Learn more about Fabric](https://docs.fabfile.org/en/stable/index.html) - [PyWinRM](https://github.com/diyan/pywinrm) -- [Pulumi - IaC Tool](https://www.pulumi.com/docs/reference/pkg/python/pulumi/) \ No newline at end of file +- [Pulumi - IaC Tool](https://www.pulumi.com/docs/reference/pkg/python/pulumi/)