Posted on:
Last modified:
httpx 是新一代的 Python http 请求库,它几乎和 requests 的 API 无缝兼容,几乎不用改代码。相对于 requests 来说,有以下优点:
还有就是 requests 有比较明显的内存泄漏,目前我还没有测试过 httpx,所以就不列到优点里了。
httpx 不支持在 client.request 中使用 proxies,必须在 Client 初始化时候指定,这样做应该是考虑到链接池的实现。
httpx 可以直接 post json
httpx.post("http://example.com", json={"foo": "bar"})
httpx 可以直接按照字典设置和读取 cookies
httpx.post("http://example.com", cookies={"SESSION_ID": "foobar"})
r = httpx.get("http://example.com/login")
print(r.cookies["SESSION_ID"])
© 2016-2022 Yifei Kong. Powered by ynotes
All contents are under the CC-BY-NC-SA license, if not otherwise specified.
Opinions expressed here are solely my own and do not express the views or opinions of my employer.