日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python中使用httpx模块详解

發(fā)布時間:2025/3/19 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python中使用httpx模块详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

導(dǎo)入httpx

In?[25]:?import?httpx

獲取一個網(wǎng)頁

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中傳遞參數(shù)

  • 在請求URL中傳遞查詢參數(shù),請使用params關(guān)鍵字

  • In?[41]:?r?=?httpx.get("https://httpbin.org/get",?params=params)In?[42]:?r Out[42]:?<Response?[200?OK]>#?檢查發(fā)出請求結(jié)果的`URL` In?[43]:?r.url Out[43]:?URL('https://httpbin.org/get?key1=value2&key2=value2')
  • 將項目列表作為值傳遞

  • In?[44]:?params?=?{"key1":?"value2",?"key2":?["value2",?"value3"]}In?[45]:?r?=?httpx.get("https://httpbin.org/get",?params=params)In?[46]:?r.url Out[46]:?URL('https://httpbin.org/get?key1=value2&key2=value2&key2=value3')

    響應(yīng)內(nèi)容

    HTTPX將自動處理響應(yīng)內(nèi)容解碼為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'#?設(shè)置需要使用的編碼 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響應(yīng)內(nèi)容

    通常, Web API 響應(yīng)將被編碼為JSON

    In?[59]:?r?=?httpx.get("https://api.github.com/events") In?[60]:?r.json()

    自定義header

    要在傳出請求中包含其他標頭,請使用header關(guān)鍵字參數(shù):

    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ā)送表單數(shù)據(jù)

    某些類型的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" }

    表單編碼的數(shù)據(jù)還可以包括給定鍵的多個值:

    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編碼數(shù)據(jù)

    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" }

    響應(yīng)狀態(tài)碼

    檢查響應(yīng)的HTTP狀態(tài)碼

    In?[82]:?r?=?httpx.get("https://httpbin.org/get")In?[83]:?r.status_code Out[83]:?200

    HTTPX還包括一個簡單的快捷方式,用于通過其文本短語訪問狀態(tài)代碼

    In?[84]:?r.status_code?==?httpx.codes.OK Out[84]:?True

    針對任何客戶端或服務(wù)器錯誤響應(yīng)(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'

    響應(yīng)Headers

    響應(yīng)標頭可作為類似于字典的接口使用

    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數(shù)據(jù)類型是不區(qū)分大小寫的,所以你可以使用任何資本

    In?[89]:?r.headers["Content-Type"] Out[89]:?'application/json'In?[90]:?r.headers.get("content-type") Out[90]:?'application/json'

    流響應(yīng)

    可以流式傳輸響應(yīng)的二進制內(nèi)容

    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

    可以輕松訪問響應(yīng)中設(shè)置的任何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參數(shù):

    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?按 域訪問設(shè)置

    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響應(yīng)的屬性可用于檢查所有后續(xù)重定向,它包含遵循他們的順序的所有重定向響應(yīng)的列表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參數(shù)修改默認的重定向處理:

    In?[113]:?r?=?httpx.head('http://github.com/',?allow_redirects=True)In?[114]:?r.url Out[114]:?URL('https://github.com/')

    認證方式

  • HTTPX支持基本和摘要HTTP身份驗證

  • In?[122]:?httpx.get("https://example.com",?auth=("my_user",?"password123"))...: Out[122]:?<Response?[200?OK]>
  • 摘要式身份驗證的憑據(jù)

  • In?[123]:?auth?=?httpx.DigestAuth("my_user",?"password123")In?[124]:?httpx.get("https://example.com",?auth=auth) Out[124]:?<Response?[200?OK]>

    總結(jié)

    以上是生活随笔為你收集整理的Python中使用httpx模块详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。