{"id":264,"date":"2023-03-23T22:50:49","date_gmt":"2023-03-23T14:50:49","guid":{"rendered":"https:\/\/chenyl.fun\/?post_type=docs&#038;p=264"},"modified":"2023-03-23T22:50:49","modified_gmt":"2023-03-23T14:50:49","slug":"python%e5%ae%9e%e7%8e%b0httpclienthttp-client%e8%ae%bf%e9%97%ae%e5%9c%b0%e5%9d%80","status":"publish","type":"docs","link":"https:\/\/chenyl.fun\/?docs=python%e5%ae%9e%e7%8e%b0httpclienthttp-client%e8%ae%bf%e9%97%ae%e5%9c%b0%e5%9d%80","title":{"rendered":"python\u5b9e\u73b0httpclient(http.client)\u8bbf\u95ee\u5730\u5740"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import http.client as hc\n\n# java\u5f00\u53d1\u8005\u601d\u8def\u7684\u9519\u8bef\u793a\u8303\ndef sendRequest():\n    con = hc.HTTPConnection(host=\"cn.bing.com\")\n    con.request(method='GET', url='https:\/\/cn.bing.com\/robots.txt')\n    response = con.getresponse()\n    restudy = bytearray(10240)\n    i = response.readinto(restudy)\n    res = bytes(restudy).decode('utf-8')\n    # restudy.zfill(1024)\n    while i != 0:\n        i = response.readinto(restudy)\n        res = res + bytes(restudy).decode('utf-8')\n        # restudy.zfill(1024)\n    print(res)\n    response.flush()\n    response.close()\n    con.close()\n\n\ndef chatAns():\n    # \u8bbe\u7f6e\u8981\u8bbf\u95ee\u7684\u94fe\u63a5\u548c\u8bf7\u6c42\u65b9\u6cd5\n    conn = hc.HTTPSConnection(\"cn.bing.com\")\n    conn.request(\"GET\", \"\/robots.txt\")\n\n    # \u83b7\u53d6\u54cd\u5e94\n    res = conn.getresponse()\n\n    # \u8f93\u51fa\u54cd\u5e94\u72b6\u6001\u7801\n    print(res.status)\n\n    # \u8f93\u51fa\u54cd\u5e94\u5934\u90e8\u4fe1\u606f\n    print(res.getheaders())\n\n    # \u8f93\u51fa\u54cd\u5e94\u6b63\u6587\n    print(res.read().decode(\"utf-8\"))\n\n\nif __name__ == '__main__':\n    print(\"n\")\n    chatAns()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"doc_category":[28],"doc_tag":[],"_links":{"self":[{"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/docs\/264"}],"collection":[{"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chenyl.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=264"}],"version-history":[{"count":1,"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/docs\/264\/revisions"}],"predecessor-version":[{"id":265,"href":"https:\/\/chenyl.fun\/index.php?rest_route=\/wp\/v2\/docs\/264\/revisions\/265"}],"wp:attachment":[{"href":"https:\/\/chenyl.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=264"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/chenyl.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fdoc_category&post=264"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/chenyl.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fdoc_tag&post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}