Advertisement
teslariu

admin_web

Aug 28th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import requests
  5. url = "http://localhost:5000/alumnos"
  6.  
  7. while True:
  8.     print("""  \n Administración de alumnos
  9.     ======================================
  10.     1. Agregar un alumno
  11.     2. Modificar datos de un alumno
  12.     3. Listar alumnos
  13.     4. Eliminar un alumno
  14.     5. Salir
  15.     """)
  16.    
  17.     opcion = input("Seleccione una opción: ")
  18.    
  19.     if opcion == "1":
  20.         nombre = input("Ingrese el nombre: ")
  21.         cursos = int(input("Ingrese los cursos: "))
  22.         datos = {'nombre':nombre, 'cursos':cursos}
  23.         r = requests.post(url, json=datos)
  24.         print(f"Código de respuesta: {r.status_code}")
  25.         print(r.json())
  26.        
  27.     elif opcion == "2":
  28.         id_alumno = int(input("Ingrese el id: "))
  29.         datos = {'id':id_alumno, 'nombre':None, 'cursos':None}
  30.         cambiar = input("¿Desea modificar el nombre (y/N)?: ")
  31.         if cambiar.lower() == "y":
  32.             datos['nombre'] = input("Ingrese el nuevo nombre: ")
  33.         cambiar = input("¿Desea modificar los cursos (y/N)?: ")
  34.         if cambiar.lower() == "y":
  35.             datos['cursos'] = input("Ingrese los nuevos cursos: ")
  36.         r = requests.put(url, json=datos)
  37.         print(f"Código de respuesta: {r.status_code}")
  38.         print(r.json())
  39.        
  40.     elif opcion == "3":
  41.         r = requests.get(url)
  42.         if r.status_code == 200:
  43.             alumnos = r.json()["alumnos"]
  44.             if not alumnos:
  45.                 print("No hay alumnos")
  46.             else:
  47.                 for alumno in alumnos:
  48.                     for k,v in alumno.items():
  49.                         print(k,v, end= " ")
  50.                     print()
  51.         else:
  52.             print("Error. No se puede imprimir la lista de alumnos")
  53.             print(f"Código de respuesta: {r.status_code}")
  54.            
  55.            
  56.     elif opcion == "4":
  57.         id_alumno = int(input("Ingrese el id: "))
  58.         datos = {'id':id_alumno}
  59.         r = requests.delete(url, json=datos)
  60.         print(f"Código de respuesta: {r.status_code}")
  61.         print(r.json())
  62.        
  63.     elif opcion == "5":
  64.         print("Cerrando conexión con el servidor...")
  65.         break
  66.        
  67.     else:
  68.         print("Opción incorrecta")
  69.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement