translated

This commit is contained in:
geekpi 2022-03-09 08:50:23 +08:00
parent 1372de4d8e
commit 3bd6774b31
2 changed files with 178 additions and 179 deletions

View File

@ -1,179 +0,0 @@
[#]: subject: "How to use httpx, a web client for Python"
[#]: via: "https://opensource.com/article/22/3/python-httpx"
[#]: author: "Moshe Zadka https://opensource.com/users/moshez"
[#]: collector: "lujun9972"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
How to use httpx, a web client for Python
======
The httpx package for Python is an excellent and flexible module for
interacting with HTTP.
![Digital creative of a browser on the internet][1]
The `httpx` package for Python is a sophisticated web client. Once you install it, you can use it to get data from websites. As usual, the easiest way to install it is with the `pip` utility:
```
`$ python -m pip install httpx --user`
```
To use it, import it into a Python script, and then use the `.get` function to fetch data from a web address:
```
import httpx
result = httpx.get("<https://httpbin.org/get?hello=world>")
result.json()["args"]
```
Here's the output from that simple script:
```
`    {'hello': 'world'}`
```
### HTTP response
By default, `httpx` will not raise errors on a non-200 status. 
Try this code:
```
result = httpx.get("<https://httpbin.org/status/404>")
result
```
The result:
```
`    <Response [404 NOT FOUND]>`
```
It's possible to raise a response explicitly. Add this exception handler:
```
try:
    result.raise_for_status()
except Exception as exc:
    print("woops", exc)
```
Here's the result:
```
    woops Client error '404 NOT FOUND' for url '<https://httpbin.org/status/404>'
    For more information check: <https://httpstatuses.com/404>
```
### Custom client
It is worthwhile to use a custom client for anything but the simplest script. Aside from nice performance improvements, such as connection pooling, this is a good place to configure the client.
For example, you can set a custom base URL:
```
client = httpx.Client(base_url="<https://httpbin.org>")
result = client.get("/get?source=custom-client")
result.json()["args"]
```
Sample output:
```
`    {'source': 'custom-client'}`
```
This is useful for a typical scenario where you use the client to talk to a specific server. For example, using both `base_url` and `auth`, you can build a nice abstraction for an authenticated client:
```
client = httpx.Client(
    base_url="<https://httpbin.org>",
    auth=("good_person", "secret_password"),
)
result = client.get("/basic-auth/good_person/secret_password")
result.json()
```
Output:
```
`    {'authenticated': True, 'user': 'good_person'}`
```
One of the nicer things you can use this for is constructing the client at a top-level "main" function and then passing it around. This lets other functions use the client and lets them get unit-tested with a client connected to a local WSGI app.
```
def get_user_name(client):
    result = client.get("/basic-auth/good_person/secret_password")
    return result.json()["user"]
get_user_name(client)
    'good_person'
def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'application/json')])
    return [b'{"user": "pretty_good_person"}']
fake_client = httpx.Client(app=application, base_url="<https://fake-server>")
get_user_name(fake_client)
```
Output:
```
`    'pretty_good_person'`
```
### Try httpx
Visit [python-httpx.org][2] for more information, documentation, and tutorials. I've found it to be an excellent and flexible module for interacting with HTTP. Give it a try and see what it can do for you.
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/3/python-httpx
作者:[Moshe Zadka][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/moshez
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_web_internet_website.png?itok=g5B_Bw62 (Digital creative of a browser on the internet)
[2]: https://www.python-httpx.org/

View File

@ -0,0 +1,178 @@
[#]: subject: "How to use httpx, a web client for Python"
[#]: via: "https://opensource.com/article/22/3/python-httpx"
[#]: author: "Moshe Zadka https://opensource.com/users/moshez"
[#]: collector: "lujun9972"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
如何使用 httpx一个 Python web 客户端
======
Python 的 httpx 包是一个用于 HTTP 交互的一个优秀且灵活的模块。
![Digital creative of a browser on the internet][1]
Python 的 `httpx` 包是一个复杂的 web 客户端。当你安装它后,你就可以用它来从网站上获取数据。像往常一样,安装它的最简单方法是使用 `pip` 工具:
```
`$ python -m pip install httpx --user`
```
要使用它,把它导入到 Python 脚本中,然后使用 `.get` 函数从一个 web 地址获取数据:
```
import httpx
result = httpx.get("<https://httpbin.org/get?hello=world>")
result.json()["args"]
```
下面是这个简单脚本的输出:
```
` {'hello': 'world'}`
```
### HTTP 响应
默认情况下,`httpx` 不会在非 200 状态下引发错误。
试试这个代码:
```
result = httpx.get("<https://httpbin.org/status/404>")
result
```
结果是:
```
` <Response [404 NOT FOUND]>`
```
可以明确地返回一个响应。添加这个异常处理:
```
try:
result.raise_for_status()
except Exception as exc:
print("woops", exc)
```
下面是结果:
```
woops Client error '404 NOT FOUND' for url '<https://httpbin.org/status/404>'
For more information check: <https://httpstatuses.com/404>
```
### 自定义客户端
除了最简单的脚本之外,使用一个自定义的客户端是值得的。除了不错的性能改进,比如连接池,这是一个配置客户端的好地方。
例如, 你可以设置一个自定义的基本 URL
```
client = httpx.Client(base_url="<https://httpbin.org>")
result = client.get("/get?source=custom-client")
result.json()["args"]
```
输出示例:
```
` {'source': 'custom-client'}`
```
这对一个典型的场景很有用,你用客户端与一个特定的服务器对话。例如,使用 `base_url``auth`,你可以为认证的客户端建立一个漂亮的抽象:
```
client = httpx.Client(
base_url="<https://httpbin.org>",
auth=("good_person", "secret_password"),
)
result = client.get("/basic-auth/good_person/secret_password")
result.json()
```
输出:
```
` {'authenticated': True, 'user': 'good_person'}`
```
你可以用它来做一件更好的事情,就是在顶层的 “main” 函数中构建客户端,然后把它传递给其他函数。这可以让其他函数使用客户端,并让它们与连接到本地 WSGI 应用的客户端进行单元测试。
```
def get_user_name(client):
result = client.get("/basic-auth/good_person/secret_password")
return result.json()["user"]
get_user_name(client)
'good_person'
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'application/json')])
return [b'{"user": "pretty_good_person"}']
fake_client = httpx.Client(app=application, base_url="<https://fake-server>")
get_user_name(fake_client)
```
输出:
```
` 'pretty_good_person'`
```
### 尝试 httpx
请访问 [python-httpx.org][2] 了解更多信息、文档和教程。我发现它是一个与 HTTP 交互的优秀而灵活的模块。试一试,看看它能为你做什么。
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/3/python-httpx
作者:[Moshe Zadka][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/moshez
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_web_internet_website.png?itok=g5B_Bw62 (Digital creative of a browser on the internet)
[2]: https://www.python-httpx.org/