Python HTTP module defines the classes which provide the client-side of the HTTP and HTTPS protocols. In most of the programs, the HTTP module is not directly used and is clubbed with the urllib
module to handle URL connections and interaction with HTTP requests. Today we will learn how to use a Python HTTP client to fire HTTP request and then parse response status and get response body data.
Python HTTP模块定义了提供HTTP和HTTPS协议客户端的类。 在大多数程序中,HTTP模块不是直接使用,而是与urllib
模块组合在一起以处理URL连接以及与HTTP请求的交互。 今天,我们将学习如何使用Python HTTP客户端来触发HTTP请求,然后解析响应状态并获取响应主体数据。
Python HTTP客户端 (Python HTTP Client)
In this post on python HTTP module, we will try attempting making connections and making HTTP requests like GET, POST and PUT. Let’s get started.
在python HTTP模块的这篇文章中,我们将尝试尝试建立连接并发出GET,POST和PUT之类的HTTP请求。 让我们开始吧。
建立HTTP连接 (Making HTTP Connections)
We will start with the simplest thing HTTP module can do. We can easily make HTTP connections using this module. Here is a sample program:
我们将从HTTP模块可以做的最简单的事情开始。 我们可以使用此模块轻松建立HTTP连接。 这是一个示例程序:
import http.client
connection = http.client.HTTPConnection('www.python.org', 80, timeout=10)
print(connection)
Let’s see the output for this program:
![python http connection](https://i-blog.csdnimg.cn/blog_migrate/ea88771977649ded3fc1db2cba8c0cd9.png)
In this script, we connected to the URL on Port 80 with a specific timeout.
让我们看一下该程序的输出:
在此脚本中,我们以特定的超时时间连接到端口80上的URL。