Advertisement
teslariu

Untitled

Dec 10th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. import requests
  6. url = "http://localhost:5000/alumno"
  7.  
  8. while True:
  9.     print("""
  10.        \nAdministración de alumnos
  11.          -------------------------
  12.          1. Agregar un alumno
  13.          2. Modificar un alumno
  14.          3. Listar alumnos
  15.          4. Eliminar un alumno
  16.          5.Salir
  17.    """)
  18.     opcion = input("Seleccione una opción: ")
  19.    
  20.     if opcion == "1":
  21.         nombre = input("Ingrese el nombre del alumno: ")
  22.         cursos = input("Ingrese la cantidad de cursos: ")
  23.         datos = {"nombre":nombre, "cursos":cursos}
  24.         r = requests.post(url, json=datos)
  25.         print("Server code response:", r.status_code)
  26.         print("Server content response:", r.json())
  27.        
  28.     elif opcion == "2":
  29.         while True:
  30.             try:
  31.                 id_alumno = int(input("Ingrese el ID del alumno: "))
  32.                 break
  33.             except ValueError:
  34.                 print("Debe ingresar un nro entero")
  35.                
  36.         datos = {"id":id_alumno, "nombre":None, "cursos":None}
  37.        
  38.         cambio = input("¿Desea modificar el nombre (y/N)? ")
  39.         if cambio.casefold() == "y":
  40.             datos['nombre'] = input("Ingrese el nuevo nombre: ")
  41.            
  42.         cambio = input("¿Desea modificar los cursos (y/N)? ")
  43.         if cambio.casefold() == "y":
  44.             datos['cursos'] = input("Ingrese la cantidad de cursos: ")
  45.         r = requests.put(url, json=datos)
  46.         print("Server code response:", r.status_code)
  47.         print("Server content response:", r.json())
  48.        
  49.     elif opcion == "3":
  50.         r = requests.get(url)
  51.         if r.status_code == 200:
  52.             print("Lista de alumnos:")
  53.             print("-----------------")
  54.             for alumno in r.json()["alumnos"]:
  55.                 print(alumno)
  56.         else:
  57.             print("No se pudo imprimir la lista de alumnos")
  58.             print("Server code response:", r.status_code)
  59.            
  60.     elif opcion == "4":
  61.         while True:
  62.             try:
  63.                 id_alumno = int(input("Ingrese el ID del alumno: "))
  64.                 break
  65.             except ValueError:
  66.                 print("Debe ingresar un nro entero")
  67.         datos = {"id":id_alumno}
  68.         r = requests.delete(url,json=datos)
  69.         print("Server code response:", r.status_code)
  70.         print("Server content response:", r.json())
  71.        
  72.     elif opcion == "5":
  73.         print("Finalizando sesión...")
  74.        
  75.     else:
  76.         print("Opción incorrecta")
  77.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement