3.3. Cliente HTTP

Python le permite escribir y ejecutar vía script Python de un cliente HTTP de forma local.

3.3.1. Ejecución en script

Usted puede escribir y ejecutar un simple cliente HTTP usando la librería http.client desde línea de comando, con el siguiente código fuente:

import http.client
import sys


def run(http_server):
    """Run HTTP Client"""
    print("HTTP Client is starting...")

    connection = None
    try:
        # create a connection
        connection = http.client.HTTPConnection(http_server, timeout=10)

        while 1:
            command = input(
                '\nEnter a input command (example GET test.html, type "exit" to end it): '
            )
            command = command.split()

            if command[0] == "exit":  # type exit to end it
                print("\nStopping web client....")
                break

            # request command to server
            connection.request(command[0], command[1])

            # get response from server
            response = connection.getresponse()

            # print server response and data
            print(response.status, response.reason)
            data_received = response.read()
            print(data_received)
        if connection:
            connection.close()
    except KeyboardInterrupt:
        print(" <Ctrl-C> entered, stopping HTTP Client...")
        if connection:
            connection.close()


if __name__ == "__main__":
    """Starting HTTP Client"""

    if not sys.argv[1:]:
        print(
            " Fatal: You forgot to include the URL like 127.0.0.1:8085 from the httpserver.py module on the command line."
        )
        print(f"Usage: python3 {sys.argv[0]} IP:PORT")
        sys.exit(2)
    else:
        # get http server ip, for example 127.0.0.1:8085
        http_server = sys.argv[1]
    run(http_server)
else:
    print("This program is bad configured, you should be call to the module...")

Guarde el archivo httpclient.py y ejecutándolo con el siguiente comando:

python3 httpclient.py

Importante

Usted puede descargar el código usado en esta sección haciendo clic en el siguiente enlace:

Truco

Para ejecutar el código httpclient.py, abra una consola de comando, acceda al directorio donde se encuentra el programa:

proyectos/
└── http/
    └── httpclient.py

Si tiene la estructura de archivo previa, entonces ejecute el siguiente comando:

python3 httpclient.py

3.3.2. Clientes externos

Existen varias herramientas clientes del protocolo HTTP:


Ver también

Consulte la sección de lecturas suplementarias del entrenamiento para ampliar su conocimiento en esta temática.


¿Cómo puedo ayudar?

¡Mi soporte está aquí para ayudar!

Mi horario de oficina es de lunes a sábado, de 9 AM a 5 PM. UTM - Madrid, España.

La hora aquí es actualmente 7:35 PM UTM.

Mi objetivo es responder a todos los mensajes dentro de un día hábil.

Contrata mi increíble soporte profesional