{"id":237704,"date":"2022-03-28T08:01:10","date_gmt":"2022-03-28T00:01:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=237704"},"modified":"2022-03-14T10:03:50","modified_gmt":"2022-03-14T02:03:50","slug":"httpx-python-web","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/httpx-python-web.html","title":{"rendered":"httpx\uff1a\u4e00\u4e2a Python Web \u5ba2\u6237\u7aef"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPython \u7684 httpx \u5305\u662f\u4e00\u4e2a\u7528\u4e8e HTTP \u4ea4\u4e92\u7684\u4e00\u4e2a\u4f18\u79c0\u4e14\u7075\u6d3b\u7684\u6a21\u5757\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Python \u7684 httpx<\/span> \u5305\u662f\u4e00\u4e2a\u590d\u6742\u7684 Web \u5ba2\u6237\u7aef\u3002\u5f53\u4f60\u5b89\u88c5\u5b83\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u7528\u5b83\u6765\u4ece\u7f51\u7ad9\u4e0a\u83b7\u53d6\u6570\u636e\u3002\u50cf\u5f80\u5e38\u4e00\u6837\uff0c\u5b89\u88c5\u5b83\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528 pip<\/span> \u5de5\u5177\uff1a<\/p>\n

$ python -m pip install httpx --user\r\n<\/pre>\n

\u8981\u4f7f\u7528\u5b83\uff0c\u628a\u5b83\u5bfc\u5165\u5230 Python \u811a\u672c\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 .get<\/span> \u51fd\u6570\u4ece\u4e00\u4e2a web \u5730\u5740\u83b7\u53d6\u6570\u636e\uff1a<\/p>\n

import httpx\r\nresult = httpx.get(\"https:\/\/httpbin.org\/get?hello=world\")\r\nresult.json()[\"args\"]\r\n<\/pre>\n

\u4e0b\u9762\u662f\u8fd9\u4e2a\u7b80\u5355\u811a\u672c\u7684\u8f93\u51fa\uff1a<\/p>\n

    {'hello': 'world'}\r\n<\/pre>\n
HTTP \u54cd\u5e94<\/strong><\/div>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0chttpx<\/span> \u4e0d\u4f1a\u5728\u975e 200 \u72b6\u6001\u4e0b\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n

\u8bd5\u8bd5\u8fd9\u4e2a\u4ee3\u7801\uff1a<\/p>\n

result = httpx.get(\"https:\/\/httpbin.org\/status\/404\")\r\nresult\r\n<\/pre>\n

\u7ed3\u679c\u662f\uff1a<\/p>\n

    \r\n<\/pre>\n

\u53ef\u4ee5\u660e\u786e\u5730\u8fd4\u56de\u4e00\u4e2a\u54cd\u5e94\u3002\u6dfb\u52a0\u8fd9\u4e2a\u5f02\u5e38\u5904\u7406\uff1a<\/p>\n

try:\r\n    result.raise_for_status()\r\nexcept Exception as exc:\r\n    print(\"woops\", exc)\r\n<\/pre>\n

\u4e0b\u9762\u662f\u7ed3\u679c\uff1a<\/p>\n

    woops Client error '404 NOT FOUND' for url 'https:\/\/httpbin.org\/status\/404'\r\n    For more information check: https:\/\/httpstatuses.com\/404\r\n<\/pre>\n
\u81ea\u5b9a\u4e49\u5ba2\u6237\u7aef<\/strong><\/div>\n

\u9664\u4e86\u6700\u7b80\u5355\u7684\u811a\u672c\u4e4b\u5916\uff0c\u4f7f\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u5ba2\u6237\u7aef\u662f\u6709\u610f\u4e49\u7684\u3002\u9664\u4e86\u4e0d\u9519\u7684\u6027\u80fd\u6539\u8fdb\uff0c\u6bd4\u5982\u8fde\u63a5\u6c60\uff0c\u8fd9\u4e5f\u662f\u4e00\u4e2a\u914d\u7f6e\u5ba2\u6237\u7aef\u7684\u597d\u5730\u65b9\u3002<\/p>\n

\u4f8b\u5982, \u4f60\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u57fa\u672c URL\uff1a<\/p>\n

client = httpx.Client(base_url=\"https:\/\/httpbin.org\")\r\nresult = client.get(\"\/get?source=custom-client\")\r\nresult.json()[\"args\"]\r\n<\/pre>\n

\u8f93\u51fa\u793a\u4f8b\uff1a<\/p>\n

    {'source': 'custom-client'}\r\n<\/pre>\n

\u8fd9\u5bf9\u7528\u5ba2\u6237\u7aef\u4e0e\u4e00\u4e2a\u7279\u5b9a\u7684\u670d\u52a1\u5668\u5bf9\u8bdd\u7684\u5178\u578b\u573a\u666f\u5f88\u6709\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 base_url<\/span> \u548c auth<\/span>\uff0c\u4f60\u53ef\u4ee5\u4e3a\u8ba4\u8bc1\u7684\u5ba2\u6237\u7aef\u5efa\u7acb\u4e00\u4e2a\u6f02\u4eae\u7684\u62bd\u8c61\uff1a<\/p>\n

client = httpx.Client(\r\n    base_url=\"https:\/\/httpbin.org\",\r\n    auth=(\"good_person\", \"secret_password\"),\r\n)\r\nresult = client.get(\"\/basic-auth\/good_person\/secret_password\")\r\nresult.json()\r\n<\/pre>\n

\u8f93\u51fa\uff1a<\/p>\n

    {'authenticated': True, 'user': 'good_person'}\r\n<\/pre>\n

\u4f60\u53ef\u4ee5\u7528\u5b83\u6765\u505a\u4e00\u4ef6\u66f4\u68d2\u7684\u4e8b\u60c5\uff0c\u5c31\u662f\u5728\u9876\u5c42\u7684 \u201c\u4e3b\u201d \u51fd\u6570\u4e2d\u6784\u5efa\u5ba2\u6237\u7aef\uff0c\u7136\u540e\u628a\u5b83\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u3002\u8fd9\u53ef\u4ee5\u8ba9\u5176\u4ed6\u51fd\u6570\u4f7f\u7528\u5ba2\u6237\u7aef\uff0c\u5e76\u8ba9\u5b83\u4eec\u4e0e\u8fde\u63a5\u5230\u672c\u5730 WSGI \u5e94\u7528\u7684\u5ba2\u6237\u7aef\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n

def get_user_name(client):\r\n    result = client.get(\"\/basic-auth\/good_person\/secret_password\")\r\n    return result.json()[\"user\"]\r\nget_user_name(client)\r\n    'good_person'\r\ndef application(environ, start_response):\r\n    start_response('200 OK', [('Content-Type', 'application\/json')])\r\n    return [b'{\"user\": \"pretty_good_person\"}']\r\nfake_client = httpx.Client(app=application, base_url=\"https:\/\/fake-server\")\r\nget_user_name(fake_client)\r\n<\/pre>\n

\u8f93\u51fa\uff1a<\/p>\n

    'pretty_good_person'\r\n<\/pre>\n
\u5c1d\u8bd5 httpx<\/strong><\/div>\n

\u8bf7\u8bbf\u95ee python-httpx.org<\/span> \u4e86\u89e3\u66f4\u591a\u4fe1\u606f\u3001\u6587\u6863\u548c\u6559\u7a0b\u3002\u6211\u53d1\u73b0\u5b83\u662f\u4e00\u4e2a\u4e0e HTTP \u4ea4\u4e92\u7684\u4f18\u79c0\u800c\u7075\u6d3b\u7684\u6a21\u5757\u3002\u8bd5\u4e00\u8bd5\uff0c\u770b\u770b\u5b83\u80fd\u4e3a\u4f60\u505a\u4ec0\u4e48\u3002<\/p>\n


\n

via:\u00a0https:\/\/opensource.com\/article\/22\/3\/python-httpx<\/a><\/p>\n

\u4f5c\u8005\uff1aMoshe Zadka<\/a>\u00a0\u9009\u9898\uff1alujun9972<\/a>\u00a0\u8bd1\u8005\uff1ageekpi<\/a>\u00a0\u6821\u5bf9\uff1awxy<\/a><\/p>\n

\u672c\u6587\u7531\u00a0LCTT<\/a>\u00a0\u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd<\/a>\u00a0\u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

Python \u7684 \u5305\u662f\u4e00\u4e2a\u590d\u6742\u7684 Web \u5ba2\u6237\u7aef\u3002\u5f53\u4f60\u5b89\u88c5\u5b83\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u7528\u5b83\u6765\u4ece\u7f51\u7ad9\u4e0a\u83b7\u53d6\u6570\u636e\u3002\u50cf\u5f80\u5e38\u4e00\u6837\uff0c\u5b89 […]<\/p>\n","protected":false},"author":1890,"featured_media":237705,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-237704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1890"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=237704"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237704\/revisions"}],"predecessor-version":[{"id":237707,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237704\/revisions\/237707"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/237705"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=237704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=237704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=237704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}