Skip to content

Commit

Permalink
Merge pull request MichaelCade#331 from rishabkumar7/main
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelCade authored Feb 20, 2023
2 parents 092d3b9 + 3d67c82 commit b27ab88
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
4 changes: 2 additions & 2 deletions 2023.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions 2023/day46.md
Original file line number Diff line number Diff line change
@@ -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).
62 changes: 62 additions & 0 deletions 2023/day47.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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('[email protected]')

# 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.

## 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](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/)

0 comments on commit b27ab88

Please sign in to comment.