Fundamentos de Deployment y Control de Versiones

1

驴C贸mo Desplegar Aplicaciones Python?

2

Introducci贸n a WSGI y ASGI para aplicaciones Python

3

Control de versiones en Git y pr谩cticas de versionamiento en Python

4

Configuraci贸n de entornos de desarrollo para despliegue

5

Buenas pr谩cticas en el uso de variables de entorno

Configuraci贸n de Servidores en la Nube para Despliegue

6

Fundamentos de servidores y conexi贸n por SSH

7

Creaci贸n y configuraci贸n de instancias en AWS, Linode y DigitalOcean

8

Creaci贸n de instancias en AWS

9

Configuraci贸n de SSH

10

Instalaci贸n y gesti贸n de paquetes en el servidor

11

Configuraci贸n de DNS para dominios en despliegue

12

Certificados SSL con Let鈥檚 Encrypt para seguridad en producci贸n

Administraci贸n y Optimizaci贸n de Servidores para Producci贸n

13

Configuraci贸n de servidores web y aplicaciones con WSGI y ASGI

14

驴C贸mo configurar UWSGI con Python y NginX en producci贸n?

15

Configuraci贸n de Proxy Reverso en Nginx para Aplicaciones WSGI

16

Manejo de errores y configuraci贸n de logs en producci贸n

17

Monitoreo de aplicaciones Python en producci贸n usando Sentry

18

驴C贸mo configurar un archivo .env en Django para producci贸n?

Integraci贸n de Servicios Complementarios para Aplicaciones Python

19

Configuraci贸n de Bases de Datos PostgreSQL en el Servidor de la Aplicaci贸n

20

Configuraci贸n de Bases de Datos en Producci贸n con Amazon RDS

21

Servicios para archivos est谩ticos (S3, Cloudflare)

Automatizaci贸n y CI/CD para Despliegues Python

22

Automatizaci贸n de despliegue con Ansible

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
13 Hrs
2 Min
29 Seg

Introducci贸n a WSGI y ASGI para aplicaciones Python

2/22
Resources
Transcript

How do Python applications connect to web servers?

Python applications establish communication with web servers using protocols that facilitate the exchange of information. Traditionally, web servers return HTML that the browser interprets to display the web page. But traditional Python applications use the WSGI (Web Server Gateway Interface) protocol to handle requests and responses. However, with the evolution of Python towards asynchronous programming, the ASGI (Asynchronous Server Gateway Interface) protocol has become relevant. ASGI allows handling multiple requests simultaneously without the need to wait for all of them to be ready.

What is the WSGI protocol and how is it implemented?

The WSGI protocol acts as a bridge between traditional Python applications and web servers. A typical WSGI application receives a request, generates a response and returns it to the web server for processing.

Basic example of a WSGI application with Unicorn

To illustrate the use of WSGI, let's consider a simple example developed in Visual Studio Code using Unicorn, a library that allows you to run Python applications on web servers:

def app(environ, start_response): status = '200 OK' headers = [('Content-type', 'text/plain; charset=utf-8')] start_response(status, headers) return [b "Hello WSGI World"]

This code snippet defines an application that returns a basic greeting. Unicorn is used to handle the execution and connection to the server:

  1. Create a WSGIApp.py file with the above code.
  2. Install Unicorn with pip install gunicorn.
  3. Run the application:
gunicorn --workers 2 --bind 127.0.0.1.1:8000 WSGIApp:app.

When accessing http://127.0.0.1:8000, you will see the message "Hello WSGI World".

What makes the ASGI protocol different?

The ASGI protocol is essential for asynchronous applications built in Python. It facilitates handling multiple requests simultaneously, optimizing execution and taking advantage of the benefits of asynchronism.

When to choose WSGI or ASGI?

Choosing between WSGI and ASGI depends on the needs and nature of your application:

  • WSGI is ideal for synchronous applications, especially if you use frameworks like Django without asynchronous support.
  • ASGI is recommended for asynchronous applications, such as those developed with FastAPI. It is ideal if you need to handle WebSockets or read and write to disk quickly.

Which frameworks and libraries are suitable for each protocol?

Python offers multiple web development frameworks, and each one is allied with different servers, depending on their characteristics. Here are some examples:

  • Django: It can work with WSGI, using uWSGI or with Unicorn. It is also compatible with ASGI to implement asynchronism.
  • FastAPI: Not compatible with uWSGI, requires Unicorn for efficient asynchronous execution.

It is crucial to choose the right protocol and library according to the demands of your application to ensure optimal performance.

Explore the many options and configure your development environment to meet your specific needs - we encourage you to keep learning and creating new applications!

Contributions 7

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

![](https://static.platzi.com/media/user_upload/flow%20n-128fe582-4afc-42fc-badd-0b6b580395d6.jpg)
![](https://static.platzi.com/media/user_upload/flow-634a1463-357b-4951-93e4-0c47462f016d.jpg)
**驴C贸mo las aplicaciones de Python se conectan con los servidores web?** El cliente hace solicitud de un p谩gina Web al servidor El servidor regresa el archivo que es interpretado por un el Browser Python no retorna un html retorna un output o salida que debe pasar a trav茅s de un protocolo que lo convierta en html que se entrega al servidor web que luego lo env铆a al cliente El protocolo se llama WSGI que proceso un solo proceso El protocolo m谩s moderno se llama ASGI y realiza m煤ltiples procesos con await para proceso as铆ncronos. **Para ejecutar el proyecto se debe instalar:** 路 Crear el proyecto 路 Crear el entorno virtual con python3 -m venv .venv 路 Crear el archivo que ejecuta WSGI 路 Instalar gunicorn 脿 pip install gunicorn 路 Ejecutar gunicorn 脿 gunicorn 鈥搘orkers 2 鈥揵ind 127.0.0.1:8000 wsgi\_app:app **WSGI vs ASGI** Para escoger en WSGI vs ASGI se deben tomar en cuenta: 路 Tipo de aplicaci贸n 路 Manejo de I/O 路 Framework a utilizar Si se utiliza WSGI se debe utilizar 脿 Gunicorn o uWSGI Si se utiliza ASGI se debe utilizar 脿 Uvicorn o Daphne
Para replicar la estructura que ves en el ejemplo del profesor, puedes crear las carpetas y el archivo manualmente o utilizar comandos. 1. **Crear el entorno virtual**: - Aseg煤rate de estar en el directorio correcto y usa: ```bash python -m venv venv ``` 2. **Carpeta pycache**: Se genera autom谩ticamente al ejecutar c贸digo en Python. No necesitas crearla manualmente. 3. **Archivo .gitignore**: Crea este archivo manualmente. Aseg煤rate de incluir `venv/` para ignorar el entorno virtual en tu repositorio. Con estos pasos, deber铆as ver la estructura similar a la del profesor.
![](https://static.platzi.com/media/user_upload/WSGI%20-%20ASGI-5182ef91-ab3a-45c1-bf60-281433a82ad6.jpg)
![](https://static.platzi.com/media/user_upload/image-40b54ba3-fee4-4031-9094-9fa266f1e229.jpg)
```python # WSGI application # pip install gunicorn #guvicorn no es compatible con windows def app(eviron, start_response): status = '200 OK' headers= [ ("content-type", "text/plain: charset=utf-8") ] start_response(status,headers) return [b"hello WSGI world: "] ```# WSGI application# pip install gunicorndef app(eviron, start\_response):聽 聽 status = '200 OK'聽 聽 headers= \[聽 聽 聽 聽 ("content-type", "text/plain: charset=utf-8")聽 聽 聽 聽 ]聽 聽 聽 聽 start\_response(status,headers)聽 聽 return \[b"hello WSGI world: "]