Python中使用httpx模块详解
導入httpx
In?[25]:?import?httpx獲取一個網頁
In?[26]:?r?=?httpx.get("https://httpbin.org/get")In?[27]:?r Out[27]:?<Response?[200?OK]>同樣,發(fā)送HTTP POST請求:
In?[28]:?r?=?httpx.post("https://httpbin.org/post",?data={"key":?"value"})In?[29]:?r Out[29]:?<Response?[200?OK]>PUT,DELETE,HEAD和OPTIONS請求都遵循相同的方式:
In?[35]:?r?=?httpx.put("https://httpbin.org/put",?data={"key":?"value"})In?[36]:?r?=?httpx.delete("https://httpbin.org/delete")In?[37]:?r?=?httpx.head("https://httpbin.org/get")In?[38]:?r?=?httpx.options("https://httpbin.org/get")在URL中傳遞參數
在請求URL中傳遞查詢參數,請使用params關鍵字
將項目列表作為值傳遞
響應內容
HTTPX將自動處理響應內容解碼為Unicode文本
In?[52]:?r?=?httpx.get("https://www.example.org/")In?[53]:?r.text Out[53]:?'<!doctype?html>\n<html>\n<head>\n????<title>Example?Domain</title>\n\n????<meta?charset="utf-8"?/>\n????<meta?http-equiv="Content-type"?content="text/html;?charset=utf-8"?/>\n????<meta?name="viewport"?content="width=device-width,?initial-scale=1"?/>\n????<style?type="text/css">\n????body?{\n????????background-color:?#f0f0f2;\n????????margin:?0;\n????????padding:?0;\n????????font-family:?-apple-system,?system-ui,?BlinkMacSystemFont,?"Segoe?UI",?"Open?Sans",?"Helvetica?Neue",?Helvetica,?Arial,?sans-serif;\n????????\n????}\n????div?{\n????????width:?600px;\n????????margin:?5em?auto;\n????????padding:?2em;\n????????background-color:?#fdfdff;\n????????border-radius:?0.5em;\n????????box-shadow:?2px?3px?7px?2px?rgba(0,0,0,0.02);\n????}\n????a:link,?a:visited?{\n????????color:?#38488f;\n????????text-decoration:?none;\n????}\n????@media?(max-width:?700px)?{\n????????div?{\n????????????margin:?0?auto;\n????????????width:?auto;\n????????}\n????}\n????</style>????\n</head>\n\n<body>\n<div>\n????<h1>Example?Domain</h1>\n????<p>This?domain?is?for?use?in?illustrative?examples?in?documents.?You?may?use?this\n????domain?in?literature?without?prior?coordination?or?asking?for?permission.</p>\n????<p><a?href="https://www.iana.org/domains/example">More?information...</a></p>\n</div>\n</body>\n</html>\n'#?解碼 In?[54]:?r.encoding Out[54]:?'UTF-8'#?設置需要使用的編碼 In?[55]:?r.encoding?=?"ISO-8859-1"In?[56]:?print(r.headers,?r.http_version,?r.url,?r.status_code) Headers({'content-encoding':?'gzip',?'age':?'532553',?'cache-control':?'max-age=604800',?'content-type':?'text/html;?charset=UTF-8',?'date':?'Sat,?06?Jun?2020?04:22:26?GMT',?'etag':?'"3147526947+gzip"',?'expires':?'Sat,?13?Jun?2020?04:22:26?GMT',?'last-modified':?'Thu,?17?Oct?2019?07:18:26?GMT',?'server':?'ECS?(oxr/830C)',?'vary':?'Accept-Encoding',?'x-cache':?'HIT',?'content-length':?'648'})?HTTP/1.1?https://www.example.org/?200返回JSON響應內容
通常, Web API 響應將被編碼為JSON
In?[59]:?r?=?httpx.get("https://api.github.com/events") In?[60]:?r.json()自定義header
要在傳出請求中包含其他標頭,請使用header關鍵字參數:
In?[60]:?url?=?'http://httpbin.org/headers'In?[61]:?headers?=?{"user-agent":?"my-app/0.0.1"}In?[62]:?r?=?httpx.get(url,?headers=headers)發(fā)送表單數據
某些類型的HTTP請求,如POST|PUT
In?[64]:?data?=?{"key1":"value1",?"key1":?"value2"}In?[65]:?r?=?httpx.post("https://httpbin.org/post",?data=data)In?[66]:?print(r.text) {"args":?{},"data":?"","files":?{},"form":?{"key1":?"value2"},"headers":?{"Accept":?"*/*","Accept-Encoding":?"gzip,?deflate","Content-Length":?"11","Content-Type":?"application/x-www-form-urlencoded","Host":?"httpbin.org","User-Agent":?"python-httpx/0.12.1","X-Amzn-Trace-Id":?"Root=1-5edb1e61-86ac64e4f9f165244f4dbd68"},"json":?null,"origin":?"114.113.113.186","url":?"https://httpbin.org/post" }表單編碼的數據還可以包括給定鍵的多個值:
In?[72]:?data?=?{"userList":?["shuke",?"jack"]}In?[73]:?r?=?httpx.post("https://httpbin.org/post",?data=data)In?[74]:?print(r.text) {"args":?{},"data":?"","files":?{},"form":?{"userList":?["shuke","jack"]},"headers":?{"Accept":?"*/*","Accept-Encoding":?"gzip,?deflate","Content-Length":?"28","Content-Type":?"application/x-www-form-urlencoded","Host":?"httpbin.org","User-Agent":?"python-httpx/0.12.1","X-Amzn-Trace-Id":?"Root=1-5edb1fc2-33761faf662f0b832cb806bb"},"json":?null,"origin":?"114.113.113.186","url":?"https://httpbin.org/post" }發(fā)送分段文件上傳
使用HTTP分段編碼上傳文件
In?[76]:?files?=?{"upload-file":?open("/Users/shuke/Work/pha/docker-compose.yml",?"rb")}In?[77]:?r?=?httpx.post("https://httpbin.org/post",?files=files)In?[78]:?print(r.text)發(fā)送JSON編碼數據
In?[79]:?data?=?{"integer":?123,?"boolean":?True,?"list":?["a",?"b",?"c"]}In?[80]:?r?=?httpx.post("https://httpbin.org/post",?json=data)In?[81]:?print(r.text) {"args":?{},"data":?"{\"integer\":?123,?\"boolean\":?true,?\"list\":?[\"a\",?\"b\",?\"c\"]}","files":?{},"form":?{},"headers":?{"Accept":?"*/*","Accept-Encoding":?"gzip,?deflate","Content-Length":?"58","Content-Type":?"application/json","Host":?"httpbin.org","User-Agent":?"python-httpx/0.12.1","X-Amzn-Trace-Id":?"Root=1-5edb217c-cd09ef8caa55514051957eb8"},"json":?{"boolean":?true,"integer":?123,"list":?["a","b","c"]},"origin":?"114.113.113.186","url":?"https://httpbin.org/post" }響應狀態(tài)碼
檢查響應的HTTP狀態(tài)碼
In?[82]:?r?=?httpx.get("https://httpbin.org/get")In?[83]:?r.status_code Out[83]:?200HTTPX還包括一個簡單的快捷方式,用于通過其文本短語訪問狀態(tài)代碼
In?[84]:?r.status_code?==?httpx.codes.OK Out[84]:?True針對任何客戶端或服務器錯誤響應(4xx或5xx狀態(tài)代碼) 引發(fā)異常:
In?[85]:?not_found?=?httpx.get("https://httpbin.org/status/404")In?[86]:?not_found.status_code Out[86]:?404In?[87]:?not_found.rasise_for_status() --------------------------------------------------------------------------- AttributeError????????????????????????????Traceback?(most?recent?call?last) <ipython-input-87-c7b57e835640>?in?<module> ---->?1?not_found.rasise_for_status()AttributeError:?'Response'?object?has?no?attribute?'rasise_for_status'響應Headers
響應標頭可作為類似于字典的接口使用
In?[88]:?r.headers Out[88]:?Headers({'date':?'Sat,?06?Jun?2020?04:56:56?GMT',?'content-type':?'application/json',?'content-length':?'306',?'connection':?'keep-alive',?'server':?'gunicorn/19.9.0',?'access-control-allow-origin':?'*',?'access-control-allow-credentials':?'true'})該Headers數據類型是不區(qū)分大小寫的,所以你可以使用任何資本
In?[89]:?r.headers["Content-Type"] Out[89]:?'application/json'In?[90]:?r.headers.get("content-type") Out[90]:?'application/json'流響應
可以流式傳輸響應的二進制內容
In?[91]:?with?httpx.stream("GET",?"https://www.example.com")?as?r:...:?????for?data?in?r.iter_bytes():...:?????????print(data)或文字...
In?[93]:?with?httpx.stream("GET",?"https://www.example.com")?as?r:...:?????for?text?in?r.iter_text():...:?????????print(text)或逐行流文本...
In?[96]:?with?httpx.stream("GET",?"https://www.example.com")?as?r:...:?????for?text?in?r.iter_lines():...:?????????print(text)Cookies
可以輕松訪問響應中設置的任何cookie:
In?[97]:?r?=?httpx.get("http://httpbin.org/cookies/set?chocolate=chip",?allow_redirects=False)In?[98]:?r.cookies["chocolate"] Out[98]:?'chip'如果需要將Cookies包含在外發(fā)請求中,請使用cookies參數:
In?[99]:?cookies?=?{"peanut":?"butter"}In?[100]:?r?=?httpx.get("http://httpbin.org/cookies",?cookies=cookies)In?[101]:?r.json() Out[101]:?{'cookies':?{'peanut':?'butter'}}Cookies?按 域訪問設置
In?[102]:?cookies?=?httpx.Cookies()In?[103]:?cookies.set('cookie_on_domain',?'hello,?there!',?domain='httpbin.org')In?[104]:?cookies.set('cookies_off_domain',?'nope',?domain="example.org")In?[105]:?r?=?httpx.get("http://httpbin.org/cookies",?cookies=cookies)In?[106]:?r.json() Out[106]:?{'cookies':?{'cookie_on_domain':?'hello,?there!'}}URL?重定向和歷史 默認情況下,HTTPX將對重定向執(zhí)行除HEAD請求之外的任何操作。history響應的屬性可用于檢查所有后續(xù)重定向,它包含遵循他們的順序的所有重定向響應的列表GITHUB將所有HTTP請求重定向到HTTPS
In?[107]:?r?=?httpx.get("http://github.com")In?[108]:?r.url Out[108]:?URL('https://github.com')In?[109]:?r.status_code Out[109]:?200In?[110]:?r.history Out[110]:?[]您可以使用allow_redirects參數修改默認的重定向處理:
In?[113]:?r?=?httpx.head('http://github.com/',?allow_redirects=True)In?[114]:?r.url Out[114]:?URL('https://github.com/')認證方式
HTTPX支持基本和摘要HTTP身份驗證
摘要式身份驗證的憑據
總結
以上是生活随笔為你收集整理的Python中使用httpx模块详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux系统高并发kernel参数优化
- 下一篇: CI/CD 最佳实践的基本原则