Performing HTTP requests
Communicating your application with a remote server via HTTP is a common use case of asynchronous programming. The client performs a request, which is transferred across the network using the TCP/IP protocol; then, the server processes the information and sends the response back to the client.
The time to perform this operation might vary from a few milliseconds to several seconds, but in most cases it is safe to assume that this latency may be noticed by your users.
Getting ready
There are plenty of third-party web services on the internet that can be freely accessed for prototyping purposes. However, we do not want to rely on an external service because its API may change or it might even go offline.
For this recipe, we will implement our custom HTTP server, which generates a random JSON response that will be printed on our separate GUI application:
import time import json import random from http.server import HTTPServer, BaseHTTPRequestHandler class RandomRequestHandler...