可以定义一个Python函数,在HTTP请求执行期间在控制台上打印一个点。下面是一个示例代码:
import requests
def make_request(url):
response = None
try:
response = requests.get(url)
# print a dot for every 500ms while the request is executing
while not response:
print(".", end="")
time.sleep(0.5)
except requests.exceptions.RequestException as e:
print('Error:', e)
return response.json()
该函数使用Python requests库执行HTTP请求。在函数中,我们使用while循环打印点号,以便在请求处理期间在控制台上打印一些进度。我们还可以使用time库来控制点号之间的时间间隔。最后,我们返回HTTP响应的JSON格式。
要使用该函数,请调用make_request()函数并传入HTTP请求的URL作为参数。
response = make_request("http://example.com/api/data")
print(response)
在请求执行期间,您将在控制台上看到一个点,表示请求正在进行中。