{"id":234,"date":"2016-05-10T10:43:59","date_gmt":"2016-05-10T02:43:59","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=234"},"modified":"2016-05-10T10:43:59","modified_gmt":"2016-05-10T02:43:59","slug":"%e7%ac%ac14%e7%ab%a0http-web-services","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=234","title":{"rendered":"\u7b2c14\u7ae0HTTP WEB SERVICES"},"content":{"rendered":"<h2><strong>\u4e00\u3001\u4ecb\u7ecd<\/strong><\/h2>\n<p>\uff081\uff09HTTP WEB SERVER\u5c31\u662f\u548c\u8fdc\u7a0b\u670d\u52a1\u5668\u4ea4\u6362\u6570\u636e\uff0c\u9664\u4e86HTTP\u64cd\u4f5c\u5916\u4ec0\u4e48\u90fd\u4e0d\u7528\u3002\u4f7f\u7528http <code>GET<\/code>\u5f97\u5230\u6570\u636e\uff0c\u4f7f\u7528http <code>POST<\/code>\u53d1\u9001\u6570\u636e\uff0c\u4f7f\u7528http <code>PUT<\/code>\u4fee\u6539\u6570\u636e\uff0c\u4f7f\u7528http <code>DELETE<\/code>\u5220\u9664\u6570\u636e\u3002<\/p>\n<p>\uff082\uff09python3\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u5e93\u7528\u4e8eHTTP WEB SERVICES:<\/p>\n<ul>\n<li>http.client\u662f\u4e00\u4e2a\u5e95\u5c42\u5e93\uff0c\u5b83\u5b9e\u73b0\u4e86RFC2616\u2014\u2014HTTP\u534f\u8bae\u3002<\/li>\n<li>urllib.request\u662f\u5efa\u7acb\u5728http.client\u4e4b\u4e0a\u7684\u62bd\u8c61\u5c42\u3002\u5b83\u63d0\u4f9b\u4e86\u7528\u4e8e\u8bbf\u95eehttp\u548cftp\u670d\u52a1\u7684\u6807\u51c6API\uff0c\u81ea\u52a8\u5730\u8ddf\u968fHTTP\u91cd\u5b9a\u5411\u5e76\u5904\u7406\u4e00\u4e9b\u5e38\u89c1\u5f62\u5f0f\u7684HTTP\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n<p>\u8fd8\u6709\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5f00\u6e90\u5e93\uff0c\u5b83\u5b9e\u73b0\u7684HTTP\u529f\u80fd\u6bd4http.client\u5168\u9762\uff0c\u800c\u4e14\u63d0\u4f9b\u66f4\u597d\u7684\u62bd\u8c61\u5c42\uff0c\u5b83\u5c31\u662f<code>httplib2<\/code>\u3002<\/p>\n<p>httplib2\u7684\u5b89\u88c5\u6bd4\u8f83\u7b80\u5355\uff0c\u6211\u4e0b\u8f7d\u7684\u662f0.9.2\u5730\u5740\u662f\uff1ahttps:\/\/pypi.python.org\/pypi\/httplib2<\/p>\n<p>\u5b89\u88c5\u65b9\u6cd5\u5728\u547d\u4ee4\u884c\u7ec8\u7aef\u4e2d\u8f93\u5165\uff1a<\/p>\n<p><code>python3 setup.py install<\/code><\/p>\n<h2>\u4e8c\u3001HTTP\u7684\u529f\u80fd<\/h2>\n<p>\uff081\uff09caching<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u7f13\u5b58\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u4f8b\u5b50\uff0c\u5f53\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee<span style=\"text-decoration: underline; color: #ff9900;\">diveintomark.org<\/span>\u65f6\uff0c\u9875\u9762\u5305\u542b\u4e00\u4e2a\u80cc\u666f\u56fe<span style=\"text-decoration: underline; color: #ff9900;\">wearehugh.com\/m.jpg<\/span>\u3002\u5f53\u4f60\u7684\u6d4f\u89c8\u5668\u4e0b\u8f7d\u8fd9\u5f20\u56fe\u7247\u65f6\uff0c\u670d\u52a1\u5668\u5305\u542b\u5982\u4e0bHTTP\u5934\uff1a<\/p>\n<p><code> HTTP\/1.1 200 OK<\/code><br \/>\n<code>Date: Sun, 31 May 2009 17:14:04 GMT<\/code><br \/>\n<code>Server: Apache<\/code><br \/>\n<code>Last-Modified: Fri, 22 Aug 2008 04:28:16 GMT<\/code><br \/>\n<code>ETag: \"3075-ddc8d800\"<\/code><br \/>\n<code>Accept-Ranges: bytes<\/code><br \/>\n<code>Content-Length: 12405<\/code><br \/>\n<strong><code>Cache-Control: max-age=31536000, publicExpires: Mon, 31 May 2010 17:14:04 GMT<\/code><\/strong><br \/>\n<code>Connection: close<\/code><br \/>\n<code>Content-Type: image\/jpeg<\/code><\/p>\n<p><strong><code>Cache-Control<\/code><\/strong>\u548c<strong><code>Expires<\/code><\/strong>\u5934\u544a\u8bc9\u4f60\u7684\u6d4f\u89c8\u5668\u8fd9\u5f20\u56fe\u7247\u53ef\u4ee5\u7f13\u5b58\u591a\u4e45\uff0c\u7f13\u5b58\u5230\u54ea\u4e00\u5929\u3002\u5728\u8d85\u65f6\u4e4b\u524d\u518d\u6b21\u8bbf\u95ee\u8be5\u8d44\u6e90\u90fd\u4e0d\u4f1a\u4ea7\u751f\u6d41\u91cf\u3002<\/p>\n<p>\u5f53\u8d85\u51fa\u78c1\u76d8\u5927\u5c0f\u6216\u8005\u624b\u52a8\u64cd\u4f5c\uff0c\u90fd\u53ef\u4ee5\u5220\u9664\u7f13\u5b58\u3002\u6280\u672f\u4e0a\u8bb2\u5728<code>Cache-Control<\/code>\u5934\u4e2d\u6ca1\u6709<code>private<\/code>\u5173\u952e\u5b57\u7684\u8bdd\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6570\u636e\u662f\u53ef\u4ee5\u7f13\u5b58\u7684\uff01<\/p>\n<p>\u5982\u4f55\u516c\u53f8\u6216ISP\u7ef4\u62a4\u4e00\u4e2a\u7f13\u5b58\u4ee3\u7406\u3002\u5f53\u4f60\u8bbf\u95ee\u7f51\u7ad9\u65f6\uff0c\u4f60\u7684\u6d4f\u89c8\u5668\u5148\u4f1a\u5728\u672c\u5730\u7f13\u5b58\u67e5\u627e\uff0c\u5982\u679c\u6ca1\u627e\u5230\u7684\u8bdd\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e00\u4e2a\u8bf7\u6c42\u5c1d\u8bd5\u4ece\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0b\u8f7d\u3002\u5982\u679c\u7f13\u5b58\u4ee3\u7406\u62e5\u6709\u8bf7\u6c42\u7684\u6570\u636e\uff0c\u5b83\u4f1a\u4e2d\u65ad\u90a3\u4e2a\u8bf7\u6c42\u5e76\u4ece\u7f13\u5b58\u4e2d\u63d0\u53d6\u6570\u636e\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u3002\u5b9e\u9645\u4e0a\u4f60\u7684\u8bf7\u6c42\u6839\u672c\u6ca1\u6709\u79bb\u5f00\u516c\u53f8\u7684\u7f51\u7edc\u3002\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u62e5\u6709\u66f4\u5feb\u7684\u4e0b\u884c\u901f\u5ea6\u4e86\u3002<\/p>\n<p>python\u7684HTTP\u5e93\u4e0d\u652f\u6301\u7f13\u5b58\uff0c\u4f46\u662f<code>httplib2<\/code>\u652f\u6301\u3002<\/p>\n<h2>\uff082\uff09\u6700\u540e\u4fee\u6539\u7f13\u5b58<\/h2>\n<p><code> HTTP\/1.1 200 OK<\/code><br \/>\n<code>Date: Sun, 31 May 2009 17:14:04 GMT<\/code><br \/>\n<code>Server: Apache<\/code><br \/>\n<strong><code>Last-Modified: Fri, 22 Aug 2008 04:28:16 GMT<\/code><\/strong><br \/>\n<code>ETag: \"3075-ddc8d800\"<\/code><br \/>\n<code>Accept-Ranges: bytes<\/code><br \/>\n<code>Content-Length: 12405<\/code><br \/>\n<code>Cache-Control: max-age=31536000, public<\/code><br \/>\n<code>Expires: Mon, 31 May 2010 17:14:04 GMT<\/code><br \/>\n<code>Connection: close<\/code><br \/>\n<code>Content-Type: image\/jpeg<\/code><\/p>\n<p><code>Last-Modified<\/code>\uff0c\u5982\u540c\u5b83\u7684\u540d\u5b57\u4e00\u6837\uff0c\u5c31\u662f\u6700\u540e\u4fee\u6539\u65f6\u95f4\u3002\u5728\u7b2c\u4e8c\u6b21\u8bf7\u6c42\u540c\u4e00\u4e2a\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u643a\u5e26If-Modified-Since\u5728\u4f60\u8bf7\u6c42\u4e2d\uff0c<span style=\"color: #ff9900;\"><span style=\"color: #000000;\">\u5982\u679c\u6570\u636e\u5df2\u7ecf\u66f4\u6539\uff0c\u670d\u52a1\u5668\u4f1a\u5ffd\u7565\u8be5\u5b57\u6bb5\uff0c\u8fd4\u56de\u65b0\u7684\u6570\u636e\uff0c<\/span>\u72b6\u6001\u4ee3\u7801200<\/span>\u3002\u5982\u679c\u6570\u636e\u6ca1\u6709\u66f4\u6539\uff0c\u670d\u52a1\u5668\u53d1\u56de<span style=\"color: #ff9900;\">HTTP 304\u72b6\u6001<\/span>\u3002\u53ef\u4ee5\u4f7f\u7528curl\u6d4b\u8bd5\u8fd9\u90e8\u5206\uff1a<\/p>\n<p><code> you@localhost:~$ curl -I <strong>-H \"If-Modified-Since: Fri, 22 Aug 2008 04:28:16 GMT\"<\/strong> http:\/\/wearehugh.com\/m.jpg<\/code><br \/>\n<code>HTTP\/1.1 304 Not Modified<\/code><br \/>\n<code>Date: Sun, 31 May 2009 18:04:39 GMT<\/code><br \/>\n<code>Server: Apache<\/code><br \/>\n<code>Connection: close<\/code><br \/>\n<code>ETag: \"3075-ddc8d800\"<\/code><br \/>\n<code>Expires: Mon, 31 May 2010 18:04:39 GMT<\/code><br \/>\n<code>Cache-Control: max-age=31536000, public<\/code><\/p>\n<p>python\u7684HTTP\u5e93\u4e0d\u652f\u6301\u8fd9\u9879\u68c0\u67e5\uff0c\u4f46\u662f<strong><code>httplib2<\/code><\/strong>\u53ef\u4ee5\u3002<\/p>\n<h2>\uff083\uff09ETag \u68c0\u67e5<\/h2>\n<p><code>ETag<\/code>\u662f\u53e6\u4e00\u79cd\u5b8c\u6210<code>last-modified<\/code>\u68c0\u67e5\u7684\u65b9\u6cd5\u3002\u4f7f\u7528Etags\uff0c\u670d\u52a1\u5668\u53d1\u9001\u5173\u4e8e\u4f60\u6240\u8bf7\u6c42\u6570\u636e\u7684\u54c8\u5e0c\u4ee3\u7801\u5b58\u5728<code>ETag<\/code>\u4e2d\uff08\u54c8\u5e0c\u4ee3\u7684\u751f\u6210\u5b8c\u5168\u7531\u670d\u52a1\u5668\u89c9\u5b9a\uff0c\u552f\u4e00\u8981\u6c42\u662f\u5f53\u6570\u636e\u6539\u53d8\u65f6\u54c8\u5e0c\u503c\u4e5f\u8981\u6539\u53d8\uff09\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7684ETAG\uff1a<\/p>\n<p><code> HTTP\/1.1 200 OK<\/code><br \/>\n<code>Date: Sun, 31 May 2009 17:14:04 GMT<\/code><br \/>\n<code>Server: Apache<\/code><br \/>\n<code>Last-Modified: Fri, 22 Aug 2008 04:28:16 GMT<\/code><br \/>\n<strong><code>ETag: \"3075-ddc8d800\"<\/code><\/strong><br \/>\n<code>Accept-Ranges: bytes<\/code><br \/>\n<code>Content-Length: 12405<\/code><br \/>\n<code>Cache-Control: max-age=31536000, public<\/code><br \/>\n<code>Expires: Mon, 31 May 2010 17:14:04 GMT<\/code><br \/>\n<code>Connection: close<\/code><br \/>\n<code>Content-Type: image\/jpeg<\/code><\/p>\n<p>\u5728\u7b2c\u4e8c\u6b21\u8bf7\u6c42\u76f8\u540c\u6570\u636e\u65f6\uff0cETag\u54c8\u5e0c\u5305\u542b\u5728\u4f60\u8bf7\u6c42\u7684<span style=\"text-decoration: underline;\"><code>If-None-Match<\/code><\/span>\u4e2d\u3002\u5982\u679c\u6570\u636e\u6ca1\u6709\u6539\u53d8\u670d\u52a1\u5668\u4f1a\u53d1\u56de304\u72b6\u6001\u3002\u4f7f\u7528curl\u6d4b\u8bd5\u8fd9\u90e8\u5206\uff1a<\/p>\n<p><code> you@localhost:~$ curl -I<strong> -H \"If-None-Match: \\\"3075-ddc8d800\\\"\"<\/strong> http:\/\/wearehugh.com\/m.jpg \u2460<\/code><br \/>\n<code>HTTP\/1.1 304 Not Modified<\/code><br \/>\n<code>Date: Sun, 31 May 2009 18:04:39 GMT<\/code><br \/>\n<code>Server: Apache<\/code><br \/>\n<code>Connection: close<\/code><br \/>\n<code>ETag: \"3075-ddc8d800\"<\/code><br \/>\n<code>Expires: Mon, 31 May 2010 18:04:39 GMT<\/code><br \/>\n<code>Cache-Control: max-age=31536000, public<\/code><\/p>\n<p>\u6ce8\u610f\uff1aETags\u901a\u5e38\u662f\u5305\u542b\u5728\u4e00\u5bf9\u5f15\u53f7\u91cc\u7684\uff0c\u800c<strong><span style=\"text-decoration: underline;\">\u5f15\u53f7<\/span><\/strong>\u4e5f\u662fETag\u503c\u7684\u4e00\u90e8\u5206\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u5728<code>If-None-Match<\/code>\u4e2d\u5305\u542b<span style=\"color: #ff9900;\">\u5f15\u53f7<\/span>\u5e76\u53d1\u9001\u5230\u670d\u52a1\u5668\u3002<\/p>\n<p>python\u7684http\u5e93\u4e0d\u652f\u6301ETags\uff0c\u4f46\u662fhttplib2\u53ef\u4ee5\u3002<\/p>\n<h2>\uff084\uff09\u538b\u7f29<\/h2>\n<p>HTTP\u652f\u6301\u51e0\u54b1\u538b\u7f29\u7b97\u6cd5\u3002\u6700\u5e38\u7528\u7684\u4e24\u79cd\u662fgzip\u548cdeflate\u3002\u5f53\u4f60\u901a\u8fc7HTTP\u8bf7\u6c42\u8d44\u6e90\u65f6\uff0c\u53ef\u4ee5\u8981\u6c42\u670d\u52a1\u5668\u53d1\u9001\u5b83\u7684\u538b\u7f29\u683c\u5f0f\u3002\u5728\u4f60\u7684\u8bf7\u6c42\u4e2d\u5305\u542b<code>Accept-encoding<\/code>\uff0c\u5176\u4e2d\u5217\u51fa\u4e86\u54ea\u79cd\u538b\u7f29\u7b97\u6cd5\u662f\u4f60\u652f\u6301\u7684\u3002\u5982\u679c\u670d\u52a1\u5668\u652f\u6301\u4efb\u4f55\u4e00\u4e2a\u7b97\u53d1\uff0c\u5b83\u4f1a\u4ee5\u538b\u7f29\u6570\u636e\u56de\u4f20\uff08\u5305\u542b\u4e00\u4e2a<code>Content-encoding<\/code>\u5b83\u544a\u8bc9\u4f60\u4f7f\u7528\u54ea\u79cd\u7b97\u6cd5\uff09\u3002<\/p>\n<p><span style=\"color: #ff0000;\">\u5bf9\u4e8e\u670d\u52a1\u7aef\u5f00\u53d1\u8005\u7684\u91cd\u8981\u63d0\u793a<\/span>\uff1a\u786e\u5b9a\u4e00\u4e2a\u8d44\u6e90\u538b\u7f29\u524d\u540e\u6709\u4e0d\u540c\u7684Etag\u3002\u5426\u5219\uff0c\u7f13\u5b58\u670d\u52a1\u5668\u4f1a\u611f\u5230\u56f0\u60d1\u5e76\u4f7f\u7528\u538b\u7f29\u540e\u7684\u6570\u636e\u4f20\u7ed9\u5ba2\u6237\u7aef\uff0c\u800c\u4e14\u5ba2\u6237\u4e0d\u80fd\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002\u9605\u8bfbapache bug 39727\u83b7\u5f97\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u3002<\/p>\n<p>python\u7684HTTP\u5e93\u4e0d\u652f\u6301\u538b\u7f29\uff0c\u4f46\u662fhttplib2\u53ef\u4ee5\u3002<\/p>\n<h2>\uff085\uff09\u91cd\u5b9a\u5411<\/h2>\n<p>URI\u4e0d\u6539\u53d8\uff0cweb\u7ad9\u70b9\u7684\u9875\u9762\u79fb\u52a8\u5230\u65b0\u7684\u5730\u5740\uff0c\u751a\u81f3\u662fweb\u670d\u52a1\u91cd\u65b0\u6ce8\u518c\u3002\u53ef\u4ee5\u662f\u67d0\u4e2a\u9875\u91cd\u5b9a\u5411\u5230\u5176\u5b83\u9875\uff0c\u4e5f\u53ef\u4ee5\u662f\u6574\u4e2a\u7ad9\u70b9\u79fb\u52a8\u5230\u65b0\u4f4d\u7f6e\u3002<\/p>\n<p>\u6bcf\u6b21\u4eceHTTP\u670d\u52a1\u5668\u8bf7\u6c42\u4efb\u4f55\u7c7b\u578b\u7684\u8d44\u6e90\u65f6\uff0c\u670d\u52a1\u5668\u5728\u5b83\u7684\u54cd\u5e94\u4e2d\u5305\u51fd\u72b6\u6001\u4ee3\u7801\u3002<span style=\"color: #ff6600;\">\u72b6\u6001\u4ee3\u7801200<\/span>\u610f\u5473\u7740\u201c\u4f60\u8bf7\u6c42\u7684\u9875\u9762\u4e00\u5207\u6b63\u5e38\u201d\u3002<span style=\"color: #ff6600;\">\u72b6\u6001\u4ee3\u7801404<\/span>\u610f\u5473\u7740\u201c\u9875\u9762\u6ca1\u627e\u5230\u201d.\u72b6\u6001\u4ee3\u78013xx\u6307\u793a\u4e86\u4e00\u4e9b\u91cd\u5b9a\u5411\u5f62\u5f0f\uff08 Status codes in the 300\u2019s indicate some form of redirection\uff09\u3002<\/p>\n<p>HTTP\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6307\u793a\u8d44\u6e90\u5df2\u7ecf\u79fb\u52a8\u3002\u4e24\u79cd\u6700\u5e38\u7528\u7684\u72b6\u6001\u4ee3\u7801\u662f302\u548c301\u3002<span style=\"color: #ff6600;\">\u72b6\u6001\u4ee3\u7801302<\/span>\u662f<em>\u4e34\u65f6\u91cd\u5b9a\u5411<\/em>\uff0c\u5728<code>Location<\/code>\u91cc\u5b58\u653e\u65b0\u5730\u5740\u3002<span style=\"color: #ff6600;\">\u72b6\u6001\u4ee3\u7801301<\/span>\u662f<em>\u6c38\u4e45\u91cd\u5b9a\u5411<\/em>\uff0c\u5728<code>Location<\/code>\u91cc\u5b58\u653e\u65b0\u5730\u5740\u3002\u5982\u679c\u4f60\u5f97\u5230\u4e86302\u72b6\u6001\u4ee3\u7801\u548c\u4e00\u4e2a\u65b0\u5730\u5740\uff0cHTTP\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u65b0\u7684\u5730\u5740\uff0c\u4f46\u4e0b\u6b21\u8bbf\u95ee\u76f8\u540c\u5730\u5740\u65f6\uff0c\u4f1a\u91cd\u8bd5\u65e7\u5730\u5740\u3002\u4f46\u662f\u5982\u679c\u4f60\u5f97\u5230\u7684\u662f301\u72b6\u6001\u4ee3\u7801\u548c\u4e00\u4e2a\u65b0\u5730\u5740\uff0c\u5c31\u4f1a\u5047\u5b9a\u4e00\u76f4\u4f7f\u7528\u65b0\u5730\u5740\u3002<\/p>\n<p>\u5f53\u4eceHTTP\u670d\u52a1\u5668\u6536\u5230\u4e00\u4e2a\u72b6\u6001\u4ee3\u7801\u65f6<code>urllib.request<\/code>\u6a21\u5757\u81ea\u52a8\u7684\u201c\u8ddf\u8e2a\u201d\u91cd\u5b9a\u5411\uff0c\u4f46\u662f\u5b83\u4e0d\u4f1a\u544a\u8bc9\u4f60\u5b83\u505a\u4e86\u4ec0\u4e48\u3002\u4f60\u4f1a\u76f4\u63a5\u5f97\u5230\u6570\u636e\uff0c\u800c\u4e0d\u4f1a\u77e5\u9053\u4e2d\u95f4\u7ecf\u8fc7\u4e86\u91cd\u5b9a\u5411\u3002<code>urllib.request<\/code>\u5904\u7406\u4e34\u65f6\u91cd\u5b9a\u5411\u548c\u6c38\u4e45\u91cd\u5b9a\u5411\u7684\u65b9\u6cd5\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u65e0\u8bba\u662f\u5bf9\u670d\u52a1\u5668\u8fd8\u662f\u5ba2\u6237\u7aef\u90fd\u4e0d\u597d\u3002<\/p>\n<p><span style=\"color: #ff6600;\"><code>httplib2<\/code><\/span>\u5904\u7406\u6c38\u4e45\u91cd\u5b9a\u5411\u3002\u5b83\u4e0d\u4ec5\u4f1a\u544a\u8bc9\u4f60\u53d1\u751f\u4e86\u6c38\u4e45\u91cd\u5b9a\u5411\uff0c\u800c\u4e14\u4f1a\u5728\u8bf7\u6c42\u5b83\u4eec\u4e4b\u524d\u8ddf\u8e2a\u5b83\u4eec\uff0c\u672c\u5730\u7684\u5e76\u81ea\u52a8\u7684\u91cd\u5199\u91cd\u5b9a\u5411URL\u3002<\/p>\n<h2>\uff086\uff09\u5982\u4f55\u5728HTTP\u534f\u8bae\u4e0a\u83b7\u5f97\u6570\u636e<\/h2>\n<p>\u8981\u901a\u8fc7HTTP\u4e0b\u8f7d\u8d44\u6e90\uff0c\u53ea\u8981\u4e0b\u8f7d\u4e00\u6b21\u5c31\u53ef\u4ee5\uff0c\u4f46\u5982\u679c\u8981\u53cd\u590d\u7684\u4e0b\u8f7d\u540c\u4e00\u4e2a\u8d44\u6e90\u5462\uff1f\u8ba9\u6211\u4eec\u5148\u770b\u4e00\u4e2a\u5feb\u901f\u4f46\u4e0d\u597d\u7684\u4f8b\u5b50\uff1a<\/p>\n<p><code> &gt;&gt;&gt; import urllib.request<\/code><br \/>\n<code>&gt;&gt;&gt; a_url = 'http:\/\/diveintopython3.org\/examples\/feed.xml'<\/code><br \/>\n<code>&gt;&gt;&gt; data = urllib.request.urlopen(a_url).read() \u2460<\/code><br \/>\n<code>&gt;&gt;&gt; type(data) \u2461<\/code><br \/>\n<code>&lt;class 'bytes'&gt;<\/code><br \/>\n<code>&gt;&gt;&gt; print(data)<\/code><br \/>\n<code>&lt;?xml version='1.0' encoding='utf-8'?&gt;<\/code><br \/>\n<code>&lt;feed xmlns='http:\/\/www.w3.org\/2005\/Atom' xml:lang='en'&gt;<\/code><br \/>\n<code>\u00a0 &lt;title&gt;dive into mark&lt;\/title&gt;<\/code><br \/>\n<code>\u00a0 &lt;subtitle&gt;currently between addictions&lt;\/subtitle&gt;<\/code><br \/>\n<code>\u00a0 &lt;id&gt;tag:diveintomark.org,2001-07-29:\/&lt;\/id&gt;<\/code><br \/>\n<code>\u00a0 &lt;updated&gt;2009-03-27T21:56:07Z&lt;\/updated&gt;<\/code><br \/>\n<code>\u00a0 &lt;link rel='alternate' type='text\/html' href='http:\/\/diveintomark.org\/'\/&gt;<\/code><br \/>\n<code>\u2026<\/code><\/p>\n<ol>\n<li>\u5728python\u4e2d\u901a\u8fc7HTTP\u4e0b\u8f7d\u6587\u4ef6\u662f\u4e00\u4ef6\u5f02\u5e38\u7b80\u5355\u7684\u4e8b\uff1b\u5b9e\u9645\u4e0a\u53ea\u7528\u4e00\u884c\u5c31\u53ef\u4ee5\u3002<code>urllib.request<\/code>\u6a21\u5757\u6709\u4e00\u4e2a<code>urlopen()<\/code>\u51fd\u6570\uff0c\u5b83\u786e\u5b9a\u4e00\u4e2a\u4f60\u60f3\u5f97\u5230\u5730\u5740\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u50cf\uff0c\u53ef\u4ee5\u5bf9\u5b83\u4f7f\u7528<code>read()<\/code>\u5f97\u5230\u9875\u9762\u7684\u5185\u5bb9\u3002<\/li>\n<li><code>urlopen().read()<\/code>\u65b9\u6cd5\u4e00\u76f4\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ol>\n<p><strong>\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u592a\u4f4e\uff1a<\/strong><\/p>\n<p><code> &gt;&gt;&gt; from http.client import HTTPConnection<\/code><br \/>\n<code>&gt;&gt;&gt; HTTPConnection.debuglevel = 1 \u2460<\/code><br \/>\n<code>&gt;&gt;&gt; from urllib.request import urlopen<\/code><br \/>\n<code>&gt;&gt;&gt; response = urlopen('http:\/\/diveintopython3.org\/examples\/feed.xml') \u2461<\/code><br \/>\n<code>send: b'GET \/examples\/feed.xml HTTP\/1.1 \u2462<\/code><br \/>\n<code>Host: diveintopython3.org \u2463<\/code><br \/>\n<code>Accept-Encoding: identity \u2464<\/code><br \/>\n<code>User-Agent: Python-urllib\/3.1' \u2465<\/code><br \/>\n<code>Connection: close<\/code><br \/>\n<code>reply: 'HTTP\/1.1 200 OK'<\/code><br \/>\n<code>\u2026further debugging information omitted\u2026<\/code><\/p>\n<ol>\n<li>\u5c31\u50cf\u6211\u5728\u672c\u7ae0\u5f00\u5934\u6240\u8bf4\u7684\u90a3\u6837<code>urllib.request<\/code>\u4f9d\u8d56python\u5176\u5b83\u6807\u51c6\u5e93\u2014\u2014<code>http.client<\/code>\u3002\u901a\u5e38\u4f60\u4e0d\u9700\u8981\u76f4\u63a5\u4f7f\u7528<code>http.client<\/code>\uff08<code>urllib.request<\/code>\u6a21\u5757\u662f\u81ea\u52a8\u5bfc\u5165\u7684\uff09\u3002\u4f46\u5728\u8fd9\u91cc\u5bfc\u5165\u5b83\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u5728HTTPConnection\u7c7b\u4e0a\u5f00\u542fdebug\u3002<\/li>\n<li>\u73b0\u5728debugging\u6807\u8bb0\u5df2\u7ecf\u8bbe\u7f6e\uff0cHTTP\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u4f1a\u5b9e\u65f6\u6253\u5370\u51fa\u6765\u3002\u5c31\u50cf\u770b\u5230\u7684\u90a3\u6837\uff0c\u5f53\u4f60\u8bf7\u6c42ATOM FEED\uff0curllib.request\u6a21\u5757\u53d1\u6837\u4e94\u884c\u5230\u670d\u52a1\u5668\u3002<\/li>\n<li>\u7b2c\u4e00\u884c\u6307\u5b9a\u4e86\u4f60\u4f7f\u7528\u7684HTTP\u52a8\u8bcd\uff08GET\uff09\u548c\u8d44\u6e90\u8def\u5f84\u3002<\/li>\n<li>\u7b2c\u4e8c\u884c\u6307\u5b9a\u4e86\u8bf7\u6c42\u8fd9\u4e2aFEED\u7684\u57df\u540d\u3002<\/li>\n<li>\u7b2c\u4e09\u884c\u6307\u5b9a\u4e86\u4f60\u5ba2\u6237\u7aef\u7684\u538b\u7f29\u7b97\u6cd5\u3002\u5c31\u50cf\u6211\u4eec\u4e4b\u524d\u8bf4\u7684\u6837\u90a3<code>urllib.request<\/code>\u9ed8\u8ba4\u4e0d\u652f\u6301\u538b\u7f29\u3002<\/li>\n<li>\u7b2c\u56db\u884c\u6307\u5b9a\u751f\u6210\u8bf7\u6c42\u5e93\u7684\u540d\u5b57\u3002\u9ed8\u8ba4\u8fd9\u4e2a\u662f<code>Python-urllib<\/code>\u52a0\u4e0a\u4e00\u4e2a\u7248\u672c\u53f7\u3002<code>urllib.request<\/code>\u548c<code>httplib2<\/code>\u90fd\u652f\u6301\u6539\u53d8user-Agent\uff0c\u53ea\u662f\u7b80\u5355\u7684\u6dfb\u52a0User-Agent\u5230\u8bf7\u6c42\u4e2d\u5c31\u53ef\u4ee5\uff0c\u5b83\u53ef\u4ee5\u8986\u76d6\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ol>\n<p><strong>\u73b0\u5728\u770b\u4e00\u4e0b\u670d\u52a1\u5668\u7684\u54cd\u5e94\uff1a<\/strong><\/p>\n<p><code> # continued from previous example<\/code><br \/>\n<code>&gt;&gt;&gt; print(response.headers.as_string()) \u2460<\/code><br \/>\n<code>Date: Sun, 31 May 2009 19:23:06 GMT \u2461<\/code><br \/>\n<code>Server: Apache<\/code><br \/>\n<code>Last-Modified: Sun, 31 May 2009 06:39:55 GMT \u2462<\/code><br \/>\n<code>ETag: \"bfe-93d9c4c0\" \u2463<\/code><br \/>\n<code>Accept-Ranges: bytes<\/code><br \/>\n<code>Content-Length: 3070 \u2464<\/code><br \/>\n<code>Cache-Control: max-age=86400 \u2465<\/code><br \/>\n<code>Expires: Mon, 01 Jun 2009 19:23:06 GMT<\/code><br \/>\n<code>Vary: Accept-Encoding<\/code><br \/>\n<code>Connection: close<\/code><br \/>\n<code>Content-Type: application\/xml<\/code><br \/>\n<code>&gt;&gt;&gt; data = response.read() \u2466<\/code><br \/>\n<code>&gt;&gt;&gt; len(data)<\/code><br \/>\n<code>3070<\/code><\/p>\n<ol>\n<li><code>response<\/code>\u8fd4\u56de<code>urllib.request.rulopen()<\/code>\u51fd\u6570\u8bf7\u6c42\u7684\u6240\u6709HTTP\u5934\uff0c\u4ece\u670d\u52a1\u5668\u53d1\u56de\u3002\u5b83\u540c\u6837\u5305\u51fd\u4e86\u4e0b\u8f7d\u6570\u636e\u7684\u65b9\u6cd5\u3002<code><\/code><\/li>\n<li>\u5f53\u670d\u52a1\u5668\u5904\u7406\u4f60\u7684\u8bf7\u6c42\u65f6\u5b83\u4f1a\u544a\u8bc9\u4f60\u3002<\/li>\n<li>\u8fd9\u4e2a\u54cd\u5e94\u5305\u542b\u4e86<code>Last-Modified\u3002<\/code><\/li>\n<li>\u8fd9\u4e2a\u54cd\u5e94\u5305\u542b\u4e86<code>ETag<\/code>\u3002<\/li>\n<li>\u6570\u636e\u957f\u5ea6\u662f3070\u5b57\u8282\u3002\u6ce8\u610f\u4ec0\u4e48\u4e0d\u5728\u8fd9\u91cc\uff1a<code>Content-encoding<\/code>\u3002\u5bf9\u4e8e\u4f60\u7684\u8bf7\u6c42\uff0c\u4f60\u53ea\u80fd\u63a5\u53d7\u672a\u538b\u7f29\u7684\u6570\u636e\uff08<code>Accept-encoding:identity<\/code>\uff09\u3002<\/li>\n<li>\u6307\u5b9a\u4e86\u7f13\u5b58\u6700\u5927\u65f6\u95f4\u662f24\u5c0f\u65f6\uff0886400\u79d2\uff09\u3002<\/li>\n<li>\u6700\u540e\u4f7f\u7528response.read()\u4e0b\u8f7d\u5b9e\u9645\u6570\u636e\u3002\u4f7f\u7528len()\u51fd\u6570\u67e5\u770b\u4e0b\u8f7d\u4e86\u591a\u5c11\u6570\u636e\u3002<\/li>\n<\/ol>\n<p>\u5c31\u50cf\u6211\u4eec\u770b\u5230\u7684\u90a3\u6837\uff0c\u6ca1\u6709\u4f7f\u7528\u538b\u7f29\u3002\u800c\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u670d\u52a1\u5668\u662f\u652f\u6301gzip\u538b\u7f29\u7684\uff0c\u4f46HTTP\u538b\u7f29\u662f\u53ef\u9009\u7684\u3002\u6211\u4eec\u5e76\u6ca1\u6709\u8981\u6c42\u4f7f\u7528\u5b83\uff0c\u6240\u4ee5\u6ca1\u7528\u538b\u7f29\u65b9\u5f0f\u4f20\u8f93\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u672c\u6765\u6709\u53ef\u80fd\u53ea\u4e0b\u8f7d941\u4e2a\u5b57\u8282\uff0c\u4f46\u662f\u6211\u4eec\u5374\u4e0b\u8f7d\u4e863070\u4e2a\u5b57\u8282\u3002<\/p>\n<p>\u5982\u679c\u518d\u6b21\u8bf7\u6c42\u76f8\u540c\u7684\u5185\u5bb9\uff0c\u540c\u6837\u6ca1\u6709If-Modified-Since\u3001\u6ca1\u6709If-None-Match\u3001\u6ca1\u6709\u7f13\u5b58\u5e76\u4e14\u4ecd\u7136\u6ca1\u6709\u538b\u7f29\uff0c\u54cd\u5e94\u4e5f\u662f\u4e00\u6837\u3002\u5982\u679c\u53ea\u4e0b\u8f7d\u4e00\u6b21\u8fd8\u597d\uff0c\u4f46\u5982\u679c\u9891\u7e41\u7684\u53cd\u590d\u4e0b\u8f7d\uff0c\u6548\u7387\u975e\u5e38\u7684\u4f4e\u4e0b\u3002<\/p>\n<h2>\uff087\uff09\u662f\u65f6\u5019\u4ecb\u7ecdhttplib2<\/h2>\n<p>\u5728\u4f7f\u7528httplib2\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u5b89\u88c5\u5b83\uff0c\u8bbf\u95eecode.google.com\/p\/httplib2\/\u5e76\u4e0b\u8f7d\u5b83\u7684\u6700\u65b0\u7248\u672c\u3002httplib2\u6709python2\u548cpython3\u4e24\u4e2a\u7248\u672c\u3002<\/p>\n<p>\u4f7f\u7528<code>httplib2<\/code>\u521b\u5efa\u4e00\u4e2a<code>httplib2.Http<\/code>\u7c7b\u7684\u5b9e\u4f8b\uff1a<\/p>\n<p><code> &gt;&gt;&gt; import httplib2<\/code><br \/>\n<code>&gt;&gt;&gt; h = httplib2.Http('.cache') \u2460<\/code><br \/>\n<code>&gt;&gt;&gt; response, content = h.request('http:\/\/diveintopython3.org\/examples\/feed.xml') \u2461<\/code><br \/>\n<code>&gt;&gt;&gt; response.status \u2462<\/code><br \/>\n<code>200<\/code><br \/>\n<code>&gt;&gt;&gt; content[:52] \u2463<\/code><br \/>\n<code>b\"&lt;?xml version='1.0' encoding='utf-8'?&gt;\\r\\n&lt;feed xmlns=\"<\/code><br \/>\n<code>&gt;&gt;&gt; len(content)<\/code><br \/>\n<code>3070<\/code><\/p>\n<ol>\n<li><code>httplib2<\/code>\u7684\u4e3b\u8981\u63a5\u53e3\u662f<code>Http<\/code>\u5bf9\u8c61\u3002<\/li>\n<li>\u4e00\u4f46\u4f60\u6709\u4e86\u4e00\u4e2aHttp\u5bf9\u8c61\uff0c\u53d6\u56de\u6570\u636e\u53ea\u8981\u8c03\u7528request()\u65b9\u6cd5\uff0c\u53c2\u6570\u662f\u4f60\u60f3\u8981\u7684\u6570\u636e\u7684\u5730\u5740\u3002\u8fd9\u4f1a\u5bf9\u90a3\u4e2a<code>URL<\/code>\u6267\u884c\u4e00\u4e2a<code>HTTP GET<\/code>\u8bf7\u6c42\u3002<\/li>\n<li><code>request()<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e24\u4e2a\u503c\u3002\u7b2c\u4e00\u4e2a\u662f<code>httplib2.Response<\/code>\u5bf9\u8c61\uff0c\u5b83\u5305\u51fd\u4e86\u90a3\u4e2a\u670d\u52a1\u5668\u6240\u6709\u7684HTTP\u5934\u4fe1\u606f\u3002<\/li>\n<li><code>content<\/code>\u53d8\u91cf\u5305\u542b\u4e86\u5b9e\u9645\u4eceHTTP\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e\u3002\u8fd4\u56de\u7684\u6570\u636e\u505a\u4e3abytes\u5bf9\u8c61\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5b83\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u89c9\u5f97\u5b57\u7b26\u7f16\u7801\u5e76\u81ea\u5df1\u8f6c\u6362\u5b83\u3002<\/li>\n<\/ol>\n<p>\u63d0\u793a\uff1a\u4f60\u53ef\u80fd\u53ea\u9700\u8981\u4e00\u4e2a<code>httplib2.Http<\/code>\u5bf9\u8c61\uff0c\u56e0\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u5c31\u53ef\u4ee5\u5b8c\u6210\u4e0d\u540cURL\u7684\u8bf7\u6c42\uff0c\u53ea\u8981\u590d\u7528\u5b83\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<h2>\uff088\uff09httplib2\u5982\u4f55\u5904\u7406\u7f13\u5b58<\/h2>\n<p>\u5728\u5efa\u7acb<code>httplib2.Http<\/code>\u5bf9\u8c61\u65f6\uff0c\u4e00\u5b9a\u8981\u6709\u4e00\u4e2a\u76ee\u5f55\u540d\u3002\u8fd9\u662f\u56e0\u4e3a\u7f13\u5b58\u9700\u8981\u7528\u5230\u8fd9\u4e2a\u76ee\u5f55\u3002<\/p>\n<p>\u5148\u6267\u884c\u4e0b\u9762\u8fd9\u4e9b\u4ee3\u7801\uff0c\u5b83\u548c\u4e4b\u524d\u7684\u64cd\u4f5c\u6ca1\u6709\u4ec0\u4e48\u4e0d\u540c\u3002<\/p>\n<p><code># continued from the previous example<\/code><br \/>\n<code>&gt;&gt;&gt; response2, content2 = h.request('http:\/\/diveintopython3.org\/examples\/feed.xml') \u2460<\/code><br \/>\n<code>&gt;&gt;&gt; response2.status \u2461<\/code><br \/>\n<code>200<\/code><br \/>\n<code>&gt;&gt;&gt; content2[:52] \u2462<\/code><br \/>\n<code>b\"&lt;?xml version='1.0' encoding='utf-8'?&gt;\\r\\n&lt;feed xmlns=\"<\/code><br \/>\n<code>&gt;&gt;&gt; len(content2)<\/code><br \/>\n<code>3070<\/code><\/p>\n<p><strong>\u7136\u540e\u628a\u8fd9\u4e2a\u4ea4\u4e92\u7a97\u53e3\u5173\u8c03\uff0c\u91cd\u65b0\u5f00\u542f\u4e00\u4e2a\uff0c\u6267\u884c\u5982\u4e0b\u4ee3\u7801\uff1a<\/strong><\/p>\n<p><code>&gt;&gt;&gt; import httplib2<\/code><br \/>\n<code>&gt;&gt;&gt; httplib2.debuglevel = 1 \u2460<\/code><br \/>\n<code>&gt;&gt;&gt; h = httplib2.Http('.cache') \u2461<\/code><br \/>\n<code>&gt;&gt;&gt; response, content = h.request('http:\/\/diveintopython3.org\/examples\/feed.xml') \u2462<\/code><br \/>\n<code>&gt;&gt;&gt; len(content) \u2463<\/code><br \/>\n<code>3070<\/code><br \/>\n<code>&gt;&gt;&gt; response.status \u2464<\/code><br \/>\n<code>200<\/code><br \/>\n<code>&gt;&gt;&gt; response.fromcache \u2465<\/code><br \/>\n<code>True<\/code><\/p>\n<ol>\n<li>\u5f00\u542fhttplib2\u7684debug\u529f\u80fd\u3002<\/li>\n<li>\u521b\u5efa<code>httplib2.Http<\/code>\u5bf9\u8c61\u3002<\/li>\n<li>\u8bf7\u6c42\u4e00\u4e2aURL\uff0c\u4ec0\u4e48\u90fd\u6ca1\u53d1\u751f\u3002\u6ca1\u6709\u4efb\u4f55\u7f51\u7edc\u6d3b\u52a8\u3002<\/li>\n<li>\u6211\u4eec\u4ecd\u7136\u6536\u5230\u4e86\u5168\u90e8\u7684\u6570\u636e\u3002<\/li>\n<li>\u5e76\u6536\u5230\u4e86\u4e00\u4e2aHTTP\u72b6\u6001\u7801\uff0c\u6307\u793a\u8fd9\u4e2a\u8bf7\u6c42\u5df2\u7ecf\u6210\u529f\u3002<\/li>\n<li>\u6307\u793a\u54cd\u5e94\u6765\u81ea<code>httplib2<\/code>\u7684\u672c\u5730\u7f13\u5b58\u3002\u5efa\u7acb<code>httplib2.Http<\/code>\u5bf9\u8c61\u65f6\u7684\u76ee\u5f55\u540d\u7528\u6765\u4fdd\u5b58<code>httplib2<\/code>\u7684\u6240\u6709\u64cd\u4f5c\u8fc7\u7684\u7f13\u5b58\u3002<\/li>\n<\/ol>\n<p><strong>\u63d0\u793a\uff1a<\/strong>\u5982\u679c\u8981\u5f00\u542f<code>httplib2<\/code>\u7684debugging\uff0c\u9700\u8981\u8bbe\u7f6e\u6a21\u5757\u7b49\u7ea7\u5e38\u91cf\uff08<code>httplib2.debuglevel<\/code>\uff09\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684<code>httplib2.Http<\/code>\u5bf9\u8c61\u3002\u5982\u679c\u8981\u5173\u95eddebugging\uff0c\u540c\u6837\u9700\u8981\u6539\u53d8\u6a21\u5757\u7b49\u7ea7\u5e38\u91cf\uff0c\u4e4b\u540e\u65b0\u5efa\u4e00\u4e2a<code>httplib2.Http<\/code>\u5bf9\u8c61\u3002<\/p>\n<p>\u4f60\u4e4b\u524d\u5728\u8fd9\u4e2aURL\u8bf7\u6c42\u7684\u6570\u636e\uff0c\u5df2\u7ecf\u6210\u529f\uff08\u72b6\u6001\uff1a200\uff09\u3002\u670d\u52a1\u5668\u7684\u54cd\u5e94\u4e0d\u4ec5\u5305\u542b\u4e86feed\u6570\u636e\uff0c\u800c\u4e14\u8fd8\u8bbe\u7f6e\u4e86\u7f13\u5b58\u5934\u4fe1\u606f\uff0c\u544a\u77e5\u63a5\u6536\u8005\u53ef\u4ee5\u7f13\u5b58\u6700\u591a24\u5c0f\u65f6\uff08<code>Cache-Control\uff1amax-age=86400<\/code>\uff09\u3002httplib2\u652f\u6301\u7f13\u5b58\u5934\u4fe1\u606f\uff0c\u5b83\u5728<code>.cache<\/code>\u76ee\u5f55\u4e2d\u7f13\u5b58\u4e4b\u524d\u7684\u54cd\u5e94\u3002\u56e0\u4e3a\u6ca1\u6709\u8d85\u65f6\uff0c\u6240\u4ee5\u5f53\u7b2c\u4e8c\u6b21\u8bf7\u6c42\u540c\u4e00\u4e2aURL\u65f6\uff0chttplib2\u53ea\u662f\u8fd4\u56de\u7f13\u5b58\u5185\u5bb9\uff0c\u5e76\u6ca1\u6709\u4efb\u4f55\u7f51\u7edc\u6d3b\u52a8\u3002<\/p>\n<p>\u5982\u679c\u60f3\u77e5\u9053\u4e00\u4e2a\u54cd\u5e94\u662f\u5426\u6765\u81ea\u7f13\u5b58\uff0c\u53ef\u4ee5\u68c0\u67e5<span style=\"color: #339966;\"><code>response.formcache<\/code><\/span>\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u672c\u5730\u7f13\u5b58\uff0c\u5c31\u8981\u4f7f\u7528\u4e00\u4e9bHTTP\u7684\u529f\u80fd\u53bb\u786e\u5b9a\u4f60\u7684\u8bf7\u6c42\u5b9e\u9645\u662f\u5230\u8fbe\u8fdc\u7a0b\u670d\u52a1\u5668\u7684\u3002<\/p>\n<p><code> # continued from the previous example<\/code><br \/>\n<code>&gt;&gt;&gt; response2, content2 = h.request('http:\/\/diveintopython3.org\/examples\/feed.xml',<\/code><br \/>\n<code>...\u00a0\u00a0 headers={'cache-control':'no-cache'}) \u2460<\/code><br \/>\n<code>connect: (diveintopython3.org, 80) \u2461<\/code><br \/>\n<code>send: b'GET \/examples\/feed.xml HTTP\/1.1<\/code><br \/>\n<code>Host: diveintopython3.org<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $<\/code><br \/>\n<code>accept-encoding: deflate, gzip<\/code><br \/>\n<code>cache-control: no-cache'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 200 OK'<\/code><br \/>\n<code>\u2026further debugging information omitted\u2026<\/code><br \/>\n<code>&gt;&gt;&gt; response2.status<\/code><br \/>\n<code>200<\/code><br \/>\n<code>&gt;&gt;&gt; response2.fromcache \u2462<\/code><br \/>\n<code>False<\/code><br \/>\n<code>&gt;&gt;&gt; print(dict(response2.items())) \u2463<\/code><br \/>\n<code>{'status': '200',<\/code><br \/>\n<code>'content-length': '3070',<\/code><br \/>\n<code>'content-location': 'http:\/\/diveintopython3.org\/examples\/feed.xml',<\/code><br \/>\n<code>'accept-ranges': 'bytes',<\/code><br \/>\n<code>'expires': 'Wed, 03 Jun 2009 00:40:26 GMT',<\/code><br \/>\n<code>'vary': 'Accept-Encoding',<\/code><br \/>\n<code>'server': 'Apache',<\/code><br \/>\n<code>'last-modified': 'Sun, 31 May 2009 22:51:11 GMT',<\/code><br \/>\n<code>'connection': 'close',<\/code><br \/>\n<code>'-content-encoding': 'gzip',<\/code><br \/>\n<code>'etag': '\"bfe-255ef5c0\"',<\/code><br \/>\n<code>'cache-control': 'max-age=86400',<\/code><br \/>\n<code>'date': 'Tue, 02 Jun 2009 00:40:26 GMT',<\/code><br \/>\n<code>'content-type': 'application\/xml'}<\/code><\/p>\n<ol>\n<li>httplib2\u5141\u8bb8\u4f60\u6dfb\u52a0\u4efb\u610fHTTP\u5934\u4fe1\u606f\u5230request\u4e2d\u3002\u4e3a\u4e86\u4e0d\u4f7f\u7528\u7f13\u5b58\uff0c\u6dfb\u52a0<code>no-cache<\/code>\u5934\u4fe1\u606f\u5230<code>header<\/code>\u76ee\u5f55\u4e2d\uff08\u4e0d\u4ec5\u662f\u672c\u5730\u7f13\u5b58\uff0c\u7f13\u5b58\u4ee3\u7406\u670d\u52a1\u5668\u4e5f\u4e0d\u4f1a\u6709\u7f13\u5b58\uff09\u3002<\/li>\n<li>\u73b0\u5728\u4f60\u770b\u5230\u4e86<code>httplib2<\/code>\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u7f51\u7edc\u8bf7\u6c42\u3002\u56e0\u4e3a\u8bbe\u7f6e\u4e86<code>no-cache<\/code>\u5934\u4fe1\u606f\uff0c\u6240\u4ee5\u4f1a\u8df3\u8fc7\u672c\u5730\u7f13\u5b58\u5e76\u4e14\u76f4\u63a5\u4ece\u7f51\u7edc\u8bf7\u6c42\u6570\u636e\u3002<\/li>\n<li>\u4fdd\u8bc1\u8fd9\u4e2a\u54cd\u5e94\u4e0d\u662f\u6765\u81ea\u672c\u5730\u7f13\u5b58\u3002<\/li>\n<li>\u8bf7\u6c42\u6210\u529f\u3002\u518d\u6b21\u4ece\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0b\u8f7d\u4e86\u5168\u90e8\u7684feed\u3002\u5b83\u5305\u542b\u4e86\u7f13\u5b58\u5934\u4fe1\u606f\uff0c\u8fd9\u88abhttplib2\u7528\u6765\u66f4\u65b0\u672c\u5730\u7f13\u5b58\u3002\u7f13\u5b58\u5c31\u662f\u8bbe\u8ba1\u7528\u6765\u7f13\u5b58\u7684\u6700\u5927\u547d\u4e2d\u548c\u6700\u5c0f\u7684\u7f51\u7edc\u8bbf\u95ee\u3002<\/li>\n<\/ol>\n<h2>\uff089\uff09httplib2\u5982\u4f55\u5904\u7406Last-Modified\u548cETag\u5934\u4fe1\u606f<\/h2>\n<p><code>Cache-Control<\/code>\u548c<code>Expires<\/code>\u7f13\u5b58\u5934\u4fe1\u606f\u88ab\u79f0\u4f5c\u201c\u65b0\u9c9c\u6307\u793a\u5668\u201d\uff08freshness indicators\uff09\uff0c\u5b83\u4eec\u76f4\u63a5\u544a\u8bc9\u7f13\u5b58\u53ef\u4ee5\u4f7f\u7528\u591a\u4e45\u3002\u4f46\u662f\u5982\u679c\u6570\u636e\u53d1\u751f\u4e86\u6539\u53d8\u600e\u4e48\u529e\u5462\uff1fHTTP\u5b9a\u4e49\u4e86<code>Last-Modified<\/code>\u548c<code>Etag<\/code>\u5c31\u662f\u4e3a\u4e86\u8fd9\u4e2a\u76ee\u5730\u3002\u8fd9\u4e9b\u5934\u4fe1\u606f\u88ab\u53eb\u505a\u201c\u6709\u6548\u6307\u793a\u5668\u201d\uff08validators\uff09\u3002<\/p>\n<p><code> &gt;&gt;&gt; import httplib2<\/code><br \/>\n<code>&gt;&gt;&gt; httplib2.debuglevel = 1<\/code><br \/>\n<code>&gt;&gt;&gt; h = httplib2.Http('.cache')<\/code><br \/>\n<code>&gt;&gt;&gt; response, content = h.request('http:\/\/diveintopython3.org\/') \u2460<\/code><br \/>\n<code>connect: (diveintopython3.org, 80)<\/code><br \/>\n<code>send: b'GET \/ HTTP\/1.1<\/code><br \/>\n<code>Host: diveintopython3.org<\/code><br \/>\n<code>accept-encoding: deflate, gzip<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 200 OK'<\/code><br \/>\n<code>&gt;&gt;&gt; print(dict(response.items())) \u2461<\/code><br \/>\n<code>{'-content-encoding': 'gzip',<\/code><br \/>\n<code>'accept-ranges': 'bytes',<\/code><br \/>\n<code>'connection': 'close',<\/code><br \/>\n<code>'content-length': '6657',<\/code><br \/>\n<code>'content-location': 'http:\/\/diveintopython3.org\/',<\/code><br \/>\n<code>'content-type': 'text\/html',<\/code><br \/>\n<code>'date': 'Tue, 02 Jun 2009 03:26:54 GMT',<\/code><br \/>\n<code>'etag': '\"7f806d-1a01-9fb97900\"', 'last-modified': 'Tue, 02 Jun 2009 02:51:48 GMT',<\/code><br \/>\n<code>'server': 'Apache',<\/code><br \/>\n<code>'status': '200',<\/code><br \/>\n<code>'vary': 'Accept-Encoding,User-Agent'}<\/code><br \/>\n<code>&gt;&gt;&gt; len(content) \u2462<\/code><br \/>\n<code>6657<\/code><\/p>\n<ol>\n<li>\u8fd9\u6b21\u662f\u4e0b\u8f7d\u4e00\u4e2a\u9875\u9762\uff0c\u5b83\u662fHTML\u3002\u56e0\u4e3a\u8fd9\u662f\u7b2c\u4e00\u6b21\u8bf7\u6c42\u8be5\u9875\u9762\u6240\u4ee5\u4f1a\u6709\u4e00\u4e9b\u7f51\u7edc\u6d41\u91cf\u3002<\/li>\n<li>\u54cd\u5e94\u4e2d\u5305\u542b\u4e86\u591a\u79cdHTTP\u5934\u4fe1\u606f\uff0c\u4f46\u662f\u6ca1\u6709\u7f13\u5b58\u4fe1\u606f\u3002\u4e0d\u8fc7\u5b83\u5305\u542b\u6709<code>ETag<\/code>\u548c<code>Last-Modified<\/code>\u5934\u4fe1\u606f\u3002<\/li>\n<li>\u672c\u6b21\u6784\u5efa\u7684\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u8fd9\u4e2a\u9875\u9762\u7684\u5927\u5c0f\u662f6657\u5b57\u8282\u3002<\/li>\n<\/ol>\n<p><code> # continued from the previous example<\/code><br \/>\n<code>&gt;&gt;&gt; response, content = h.request('http:\/\/diveintopython3.org\/') \u2460<\/code><br \/>\n<code>connect: (diveintopython3.org, 80)<\/code><br \/>\n<code>send: b'GET \/ HTTP\/1.1<\/code><br \/>\n<code>Host: diveintopython3.org<\/code><br \/>\n<code>if-none-match: \"7f806d-1a01-9fb97900\" \u2461<\/code><br \/>\n<code>if-modified-since: Tue, 02 Jun 2009 02:51:48 GMT \u2462<\/code><br \/>\n<code>accept-encoding: deflate, gzip<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 304 Not Modified' \u2463<\/code><br \/>\n<code>&gt;&gt;&gt; response.fromcache \u2464<\/code><br \/>\n<code>True<\/code><br \/>\n<code>&gt;&gt;&gt; response.status \u2465<\/code><br \/>\n<code>200<\/code><br \/>\n<code>&gt;&gt;&gt; response.dict['status'] \u2466<\/code><br \/>\n<code>'304'<\/code><br \/>\n<code>&gt;&gt;&gt; len(content) \u2467<\/code><br \/>\n<code>6657<\/code><\/p>\n<ol>\n<li>\u518d\u6b21\u8bf7\u6c42\u76f8\u540c\u7684\u9875\u9762\uff0c\u4f7f\u7528\u76f8\u540c\u7684http\u5bf9\u50cf\u3002<\/li>\n<li><code>httplib2<\/code>\u56de\u53d1\u4e86\u4e00\u6761\u5728<code>If-None-Match<\/code>\u5934\u4fe1\u606f\u4e2d\u5305\u542b<code>ETag<\/code>\u6709\u6548\u6807\u793a\u7b26\u7684\u8bf7\u6c42\u3002<\/li>\n<li>httplib2\u540c\u6837\u4f1a\u53d1\u9001<code>Last-Modified<\/code>\u6709\u6548\u6807\u8bc6\u7b26\u3002<\/li>\n<li>\u670d\u52a1\u5668\u6536\u5230\u8bf7\u6c42\u540e\u4f1a\u68c0\u67e5\u6709\u6548\u6807\u793a\u7b26\u5e76\u53d1\u73b0\u8be5\u8bf7\u6c42\u7684\u9875\u9762\u6ca1\u6709\u6539\u53d8\uff0c\u6240\u4ee5\u56de\u53d1\u72b6\u6001304\u3002<\/li>\n<li>\u5ba2\u6237\u7aef\u6536\u5230\u6d88\u606f\u540e\u6ce8\u610f\u5230\u662f\u72b6\u6001304\uff0c\u5e76\u4ece\u672c\u5730\u7f13\u5b58\u4e2d\u8bfb\u53d6\u8fd9\u4e2a\u9875\u9762\u3002<\/li>\n<li>\u8fd9\u6709\u4e00\u70b9\u70b9\u6df7\u4e71\u3002\u56e0\u4e3a\u8fd9\u91cc\u6709\u4e24\u4e2a\u72b6\u6001\u2014\u2014304\uff08\u4ece\u670d\u52a1\u5668\u8fd4\u56de\u5b83\u5f15\u53d1httplib2\u5728\u5b83\u7684\u7f13\u5b58\u4e2d\u67e5\u627e\u3002\uff09\u548c200\uff08\u8fd9\u4e2a\u72b6\u6001\u662f\u4ece\u670d\u52a1\u5668\u8fd4\u56de\u5e76\u4e14\u662f\u548c\u7f13\u5b58\u4fdd\u5b58\u5728\u4e00\u8d77\u7684\u3002\uff09<\/li>\n<li>\u5982\u679c\u8981\u5f97\u5230\u4ece\u670d\u52a1\u5668\u8fd4\u56de\u7684\u539f\u751f\u72b6\u6001\uff0c\u90a3\u5c31\u8981\u67e5\u770bresponse.dict\uff0c\u5b83\u662f\u4e00\u4e2a\u5b9e\u9645\u5934\u4fe1\u606f\u7684\u5b57\u5178\u3002<\/li>\n<li><code>httplib2<\/code>\u5bf9\u50cf\u7684<code>request<\/code>\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u5bf9\u50cf\uff0c\u7b2c\u4e00\u4e2a\u662f\u670d\u52a1\u5668\u7684\u54cd\u5e94\u4fe1\u606f\uff0c\u5305\u542b\u5404\u79cd\u5934\u4fe1\u606f\uff0c\u7b2c\u4e8c\u4e2a\u5b9e\u9645\u6570\u636e\u3002\u4e00\u822c\u6765\u8bf4\u4e0d\u9700\u8981\u77e5\u9053\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u4ece\u7f13\u5b58\u4e2d\u5f97\u5230\u54cd\u5e94\u6570\u636e\u3002<\/li>\n<\/ol>\n<h2>\uff0810\uff09http2lib\u5982\u4f55\u5904\u7406\u538b\u7f29<\/h2>\n<p>HTTP\u652f\u6301\u51e0\u79cd\u7c7b\u578b\u7684\u538b\u7f29\uff0c\u5e38\u7528\u7684\u4e24\u79cd\u662fgzip\u548cdeflate\u3002httplib2\u8fd9\u4e24\u79cd\u90fd\u652f\u6301\u3002<\/p>\n<blockquote><p><code>&gt;&gt; response, content = h.request('http:\/\/diveintopython3.org\/')<\/code><br \/>\n<code>connect: (diveintopython3.org, 80)<\/code><br \/>\n<code>send: b'GET \/ HTTP\/1.1<\/code><br \/>\n<code>Host: diveintopython3.org<\/code><br \/>\n<code>accept-encoding: deflate, gzip \u2460<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 200 OK'<\/code><br \/>\n<code>&gt;&gt;&gt; print(dict(response.items()))<\/code><br \/>\n<code>{'-content-encoding': 'gzip', \u2461<\/code><br \/>\n<code>'accept-ranges': 'bytes',<\/code><br \/>\n<code>'connection': 'close',<\/code><br \/>\n<code>'content-length': '6657',<\/code><br \/>\n<code>'content-location': 'http:\/\/diveintopython3.org\/',<\/code><br \/>\n<code>'content-type': 'text\/html',<\/code><br \/>\n<code>'date': 'Tue, 02 Jun 2009 03:26:54 GMT',<\/code><br \/>\n<code>'etag': '\"7f806d-1a01-9fb97900\"',<\/code><br \/>\n<code>'last-modified': 'Tue, 02 Jun 2009 02:51:48 GMT',<\/code><br \/>\n<code>'server': 'Apache',<\/code><br \/>\n<code>'status': '304',<\/code><br \/>\n<code>'vary': 'Accept-Encoding,User-Agent'}<\/code><\/p><\/blockquote>\n<ol>\n<li>\u6bcf\u6b21httplib2\u53d1\u9001\u4e00\u4e2a\u8bf7\u6c42\uff0c\u5b83\u90fd\u5305\u542b\u4e00\u4e2a<code>Accept-Encoding<\/code>\u5934\u4fe1\u606f\uff0c\u7528\u6765\u544a\u8bc9\u670d\u52a1\u5668\u5b83\u53ef\u4ee5\u79cd\u7406deflate\u6216gzip\u7684\u538b\u7f29\u3002<\/li>\n<li>\u5728\u672c\u4f8b\u4e2d\u670d\u52a1\u5668\u4f7f\u7528\u4e86gzip\u538b\u7f29\u3002\u5982\u679c\u4e0d\u786e\u5b9a\u662f\u5426\u4f7f\u7528\u4e86\u538b\u7f29\uff0c\u53ef\u4ee5\u68c0\u67e5<code>response['-content-encoding']<\/code>\uff0c\u6216\u8005\u4e0d\u7ba1\u5b83\u3002<\/li>\n<\/ol>\n<h2>\uff0811\uff09httplib2\u5982\u4f55\u5904\u7406\u91cd\u5b9a\u5411<\/h2>\n<p>HTTP\u5b9a\u4e49\u4e86\u4e24\u79cd\u7c7b\u578b\u7684\u91cd\u5b9a\u5411\uff1a\u4e34\u65f6\u548c\u6c38\u4e45\u3002\u5bf9\u4e8e\u4e34\u65f6\u91cd\u5b9a\u5411\u9664\u4e86\u8ddf\u968f\u5b83\u4e4b\u5916\u4e0d\u4f1a\u505a\u5176\u5b83\u7279\u522b\u7684\u4e8b\u3002httplib2\u505a\u8fd9\u4e9b\u662f\u81ea\u52a8\u8fdb\u884c\u7684\u3002<\/p>\n<p><code>&gt;&gt;&gt; import httplib2<\/code><br \/>\n<code>&gt;&gt;&gt; httplib2.debuglevel = 1<\/code><br \/>\n<code>&gt;&gt;&gt; h = httplib2.Http('.cache')<\/code><br \/>\n<code>&gt;&gt;&gt; response, content = h.request('http:\/\/diveintopython3.org\/examples\/feed-302.xml') \u2460<\/code><br \/>\n<code>connect: (diveintopython3.org, 80)<\/code><br \/>\n<code>send: b'GET \/examples\/feed-302.xml HTTP\/1.1 \u2461<\/code><br \/>\n<code>Host: diveintopython3.org<\/code><br \/>\n<code>accept-encoding: deflate, gzip<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 302 Found' \u2462<\/code><br \/>\n<code>send: b'GET \/examples\/feed.xml HTTP\/1.1 \u2463<\/code><br \/>\n<code>Host: diveintopython3.org<\/code><br \/>\n<code>accept-encoding: deflate, gzip<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 200 OK'<\/code><\/p>\n<ol>\n<li>\u5f53\u524d\u4f7f\u7528\u7684\u8fd9\u4e2a\u5730\u5740\u662f\u4e0d\u5b58\u5728\u7684\u3002<\/li>\n<li>\u8fd9\u662f\u53d1\u9001\u4e00\u4e2a\u8bf7\u6c42\u3002<\/li>\n<li>\u54cd\u5e94\u4e86\u4e00\u4e2a302\u9519\u8bef\uff0c\u6ca1\u627e\u5230\u9875\u9762\u3002\u5728\u8fd9\u91cc\u5e76\u6ca1\u6709\u663e\u793a\u51fa\u6765\u8fd9\u4e2a\u54cd\u5e94\u5305\u542b\u4e86Location\u5934\u4fe1\u606f\uff0c\u5b83\u6307\u5411\u4e00\u4e2a\u771f\u5b9e\u7684URL\u3002<\/li>\n<li>httplib2\u7acb\u5373\u8fd4\u56de\u5e76\u201c\u8ddf\u968f\u201d\u90a3\u4e2a\u91cd\u5b9a\u5411\u5730\u5740\uff1ahttp:\/\/diveintopython3.org\/examples\/feed.xml<\/li>\n<\/ol>\n<p># \u7ee7\u7eed\u521a\u624d\u7684\u4f8b\u5b50<br \/>\n<code>&gt;&gt;&gt; response \u2460<\/code><br \/>\n<code>{'status': '200',<\/code><br \/>\n<code>'content-length': '3070',<\/code><br \/>\n<code>'content-location': 'http:\/\/diveintopython3.org\/examples\/feed.xml', \u2461<\/code><br \/>\n<code>'accept-ranges': 'bytes',<\/code><br \/>\n<code>'expires': 'Thu, 04 Jun 2009 02:21:41 GMT',<\/code><br \/>\n<code>'vary': 'Accept-Encoding',<\/code><br \/>\n<code>'server': 'Apache',<\/code><br \/>\n<code>'last-modified': 'Wed, 03 Jun 2009 02:20:15 GMT',<\/code><br \/>\n<code>'connection': 'close',<\/code><br \/>\n<code>'-content-encoding': 'gzip', \u2462<\/code><br \/>\n<code>'etag': '\"bfe-4cbbf5c0\"',<\/code><br \/>\n<code>'cache-control': 'max-age=86400', \u2463<\/code><br \/>\n<code>'date': 'Wed, 03 Jun 2009 02:21:41 GMT',<\/code><br \/>\n<code>'content-type': 'application\/xml'}<\/code><\/p>\n<ol>\n<li>\u8fd9\u91cc\u7684response\u6765\u81ea\u6700\u7ec8\u7684URL\u3002<\/li>\n<li><code>httplib2<\/code>\u6dfb\u52a0\u6700\u7ec8URL\u5230<code>response<\/code>\u5b57\u5178\u4e2d\u505a\u4e3a<code>content-location<\/code>\u3002\u8fd9\u5e76\u4e0d\u662f\u6765\u81ea\u670d\u52a1\u5668\u7684\u5934\u4fe1\u606f\uff0c\u5b83\u662fhttplib2\u6307\u5b9a\u7684\u3002<\/li>\n<li>\u8fd9\u4e2afeed\u662f\u538b\u7f29\u7684\u3002<\/li>\n<li>\u5e76\u4e14\u662f\u53ef\u7f13\u5b58\u7684\u3002<\/li>\n<\/ol>\n<p>response\u4f1a\u7ed9\u4e88\u4f60\u4e00\u4e9b\u6700\u7ec8URL\u7684\u4fe1\u606f\u3002\u65e0\u8bba\u4f60\u662f\u60f3\u8981\u4e2d\u95f4URL\u8fd8\u662f\u6700\u7ec8\u88ab\u91cd\u5b9a\u5411\u7684URL\uff0chttplib2\u90fd\u53ef\u4ee5\u4e3a\u4f60\u5f97\u5230\u90a3\u4e9b\u4fe1\u606f\u3002<\/p>\n<p><code># continued from the previous example<\/code><br \/>\n<code>&gt;&gt;&gt; response.previous \u2460<\/code><br \/>\n<code>{'status': '302',<\/code><br \/>\n<code>'content-length': '228',<\/code><br \/>\n<code>'content-location': 'http:\/\/diveintopython3.org\/examples\/feed-302.xml',<\/code><br \/>\n<code>'expires': 'Thu, 04 Jun 2009 02:21:41 GMT',<\/code><br \/>\n<code>'server': 'Apache',<\/code><br \/>\n<code>'connection': 'close',<\/code><br \/>\n<code>'location': 'http:\/\/diveintopython3.org\/examples\/feed.xml',<\/code><br \/>\n<code>'cache-control': 'max-age=86400',<\/code><br \/>\n<code>'date': 'Wed, 03 Jun 2009 02:21:41 GMT',<\/code><br \/>\n<code>'content-type': 'text\/html; charset=iso-8859-1'}<\/code><br \/>\n<code>&gt;&gt;&gt; type(response) \u2461<\/code><br \/>\n<code>&lt;class 'httplib2.Response'&gt;<\/code><br \/>\n<code>&gt;&gt;&gt; type(response.previous)<\/code><br \/>\n<code>&lt;class 'httplib2.Response'&gt;<\/code><br \/>\n<code>&gt;&gt;&gt; response.previous.previous \u2462<\/code><br \/>\n<code>&gt;&gt;&gt;<\/code><\/p>\n<ol>\n<li><code>response.previous<\/code>\u5c5e\u6027\u4fdd\u5b58\u4e86\u91cd\u5b9a\u5411\u4e4b\u524d\u539f\u59cb\u7684URL\u4fe1\u606f\u3002<\/li>\n<li><code>response<\/code>\u548c<code>response.previous<\/code>\u90fd\u662f<code>httplib2.Response<\/code>\u5bf9\u50cf\u3002<code><\/code><\/li>\n<li><code>response.previous.previous<\/code>\u4ee3\u8868\u66f4\u5411\u524d\u7684URL\u3002<\/li>\n<\/ol>\n<p><code># continued from the previous example<\/code><br \/>\n<code>&gt;&gt;&gt; response2, content2 = h.request('http:\/\/diveintopython3.org\/examples\/feed-302.xml') \u2460<\/code><br \/>\n<code>connect: (diveintopython3.org, 80)<\/code><br \/>\n<code>send: b'GET \/examples\/feed-302.xml HTTP\/1.1 \u2461<\/code><br \/>\n<code>Host: diveintopython3.org<\/code><br \/>\n<code>accept-encoding: deflate, gzip<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 302 Found' \u2462<\/code><br \/>\n<code>&gt;&gt;&gt; content2 == content \u2463<\/code><br \/>\n<code>True<\/code><\/p>\n<ol>\n<li>\u540c\u6837\u7684URL\uff0c\u540c\u6837\u7684httplib2.Http\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u4f1a\u6709\u76f8\u540c\u7684\u7f13\u5b58\u3002<\/li>\n<li>302\u7684\u54cd\u5e94\u6ca1\u6709\u88ab\u7f13\u5b58\uff0c\u6240\u4ee5httplib2\u4e3a\u76f8\u540c\u7684URL\u53d1\u9001\u4e86\u5176\u5b83\u7684\u8bf7\u6c42\u3002<\/li>\n<li>\u518d\u6b21\u6536\u5230302\u3002\u4f46\u662f\u6ce8\u610f\u4ec0\u4e48\u6ca1\u53d1\u751f\uff1a\u6ca1\u6709\u518d\u6b21\u8bf7\u6c42\u6700\u7ec8\u7684URL\uff0c\u56e0\u4e3a\u54cd\u5e94\u5df2\u7ecf\u88ab\u7f13\u5b58\u4e86\u3002\u4e00\u65e6httplib2\u6536\u5230302\u4ee3\u7801\uff0c\u5b83\u4f1a\u5728\u53d1\u51fa\u8bf7\u6c42\u524d\u5148\u67e5\u627e\u81ea\u5df1\u7684\u7f13\u5b58\uff0c\u56e0\u4e3a\u7f13\u5b58\u4e2d\u5305\u542b\u4e86\u4e00\u4e2a\u6ca1\u8fc7\u671f\u7684\u8bf7\u6c42\u5185\u5bb9\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u518d\u6b21\u8bf7\u6c42\u3002<\/li>\n<li>\u4e24\u6b21\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u76f8\u540c\u7684\u3002<\/li>\n<\/ol>\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f60\u4e0d\u5fc5\u4e3a\u4e34\u65f6\u91cd\u5b9a\u5411\u4f5c\u4efb\u4f55\u4e8b\u60c5\u3002httplib2\u4f1a\u81ea\u52a8\u7684\u8ddf\u968f\u5b83\u4eec\u5230\u53e6\u4e00\u4e2a\u9875\u9762\u3002<\/p>\n<p>\u6c38\u4e45\u91cd\u5b9a\u5411\u4e5f\u662f\u540c\u6837\u7684\u7b80\u5355\u3002<\/p>\n<p><code> # continued from the previous example<\/code><br \/>\n<code>&gt;&gt;&gt; response, content = h.request('http:\/\/diveintopython3.org\/examples\/feed-301.xml') \u2460<\/code><br \/>\n<code>connect: (diveintopython3.org, 80)<\/code><br \/>\n<code>send: b'GET \/examples\/feed-301.xml HTTP\/1.1<\/code><br \/>\n<code>Host: diveintopython3.org<\/code><br \/>\n<code>accept-encoding: deflate, gzip<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 301 Moved Permanently' \u2461<\/code><br \/>\n<code>&gt;&gt;&gt; response.fromcache \u2462<\/code><br \/>\n<code>True<\/code><\/p>\n<ol>\n<li>\u540c\u6837\u7684\u8fde\u63a5\u53ea\u4e0d\u8fc7\u670d\u52a1\u5668\u7aef\u5df2\u7ecf\u6539\u6210\u6c38\u4e45\u91cd\u5b9a\u5411\u3002<\/li>\n<li>\u8fd9\u91cc\u6709\u4e00\u4e2a301\u72b6\u6001\u7801\u3002\u6ce8\u610f\u8fd9\u91cc\u5e76\u6ca1\u6709\u8bf7\u6c42\u91cd\u5b9a\u5411\u7684URL\uff0c\u56e0\u4e3a\u5df2\u7ecf\u7f13\u5b58\u5230\u672c\u5730\u4e86\u3002<\/li>\n<li>httplib2\u8ddf\u968f\u91cd\u5b9a\u5411\u5230\u5b83\u7684\u672c\u5730\u3002<\/li>\n<\/ol>\n<p><code> # continued from the previous example<\/code><br \/>\n<code>&gt;&gt;&gt; response2, content2 = h.request('http:\/\/diveintopython3.org\/examples\/feed-301.xml') \u2460<\/code><br \/>\n<code>&gt;&gt;&gt; response2.fromcache \u2461<\/code><br \/>\n<code>True<\/code><br \/>\n<code>&gt;&gt;&gt; content2 == content \u2462<\/code><br \/>\n<code>True<\/code><\/p>\n<ol>\n<li>\u8fd9\u91cc\u6709\u4e00\u4e9b\u4e0d\u540c\uff1a\u4e00\u65e6httplib2\u8ddf\u968f\u4e86\u6c38\u4e45\u91cd\u5b9a\u5411\uff0c\u90a3\u4e48\u4ee5\u540e\u5bf9\u8be5URL\u7684\u6240\u6709\u8bf7\u6c42\uff0c\u90fd\u4f1a\u76f4\u63a5\u8f6c\u5230\u6700\u7ec8\u76ee\u7684\uff0c\u800c\u4e0d\u4f1a\u5148\u7ecf\u8fc7\u539f\u59cbURL\u3002<\/li>\n<li>\u8fd9\u4e2a\u54cd\u5e94\u662f\u6765\u81ea\u7f13\u5b58\u3002<\/li>\n<li>\u83b7\u5f97\u5230\u4e86\u5168\u90e8\u6570\u636e<\/li>\n<\/ol>\n<h2>\uff0812\uff09HTTP\u4e0d\u4ec5\u9650\u5236\u5728GET\u548cPOST<\/h2>\n<p>HTTP\u4e0d\u4ec5\u9650\u5236\u5728GET\u548cPOST\u65b9\u6cd5\u3002\u8fd8\u6709\u67d0\u4e9b\u5e38\u7528\u7c7b\u578b\u7684\u8bf7\u6c42\uff0c\u7279\u522b\u662f\u5728WEB\u6d4f\u89c8\u5668\u91cc\u3002\u800cWEB\u670d\u52a1API\u80fd\u505a\u4e00\u4e9b\u8d85\u8d8aGET\u548cPOST\u7684\u4e8b\uff0c\u50cf\u662fTwitter\u548cIdenti.ca\u90fd\u63d0\u4f9b\u4e00\u4e2a\u57fa\u4e8eHTTP\u7684API\uff0c\u7528\u4e8e\u53d1\u5e03\u548c\u66f4\u65b0\u72b6\u6001\u3002httplib2\u5c31\u662f\u8fd9\u6837\u3002<\/p>\n<p><code> # continued from the previous example<\/code><br \/>\n<code>&gt;&gt;&gt; from xml.etree import ElementTree as etree<\/code><br \/>\n<code>&gt;&gt;&gt; tree = etree.fromstring(content) \u2460<\/code><br \/>\n<code>&gt;&gt;&gt; status_id = tree.findtext('id') \u2461<\/code><br \/>\n<code>&gt;&gt;&gt; status_id<\/code><br \/>\n<code>'5131472'<\/code><br \/>\n<code>&gt;&gt;&gt; url = 'https:\/\/identi.ca\/api\/statuses\/destroy\/{0}.xml'.format(status_id) \u2462<\/code><br \/>\n<code>&gt;&gt;&gt; resp, deleted_content = h.request(url, 'DELETE') \u2463<\/code><\/p>\n<ol>\n<li>\u670d\u52a1\u5668\u8fd4\u56de\u4e00\u4e2aXML\u3002<\/li>\n<li>findtext()\u65b9\u6cd5\u67e5\u627e\u8868\u8fbe\u5f0f\u5e76\u5f97\u5230\u5b83\u7684\u6587\u672c\u5185\u5bb9\u3002\u672c\u4f8b\u4e2d\u6211\u4eec\u53ea\u67e5\u627e&lt;id&gt;\u5143\u7d20\u3002<\/li>\n<li>&lt;id&gt;\u5143\u7d20\u662f\u57fa\u4e8e\u6587\u672c\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2aURL\u53bb\u5220\u9664\u6211\u4eec\u53d1\u5e03\u7684\u72b6\u6001\u4fe1\u606f\u3002<\/li>\n<li>\u5220\u9664\u6d88\u606f\uff0c\u53ea\u8981\u7b80\u5355\u7684\u8f93\u51faHTTP DELETE\u8bf7\u6c42\u5230\u90a3\u4e2aURL\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u662f\u5b9e\u9645\u4e0a\u505a\u4e86\u4ec0\u4e48\uff1a<\/p>\n<p><code> send: b'DELETE \/api\/statuses\/destroy\/5131472.xml HTTP\/1.1 \u2460<\/code><br \/>\n<code>Host: identi.ca<\/code><br \/>\n<code>Accept-Encoding: identity<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $<\/code><br \/>\n<code>'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 401 Unauthorized' \u2461<\/code><br \/>\n<code>send: b'DELETE \/api\/statuses\/destroy\/5131472.xml HTTP\/1.1 \u2462<\/code><br \/>\n<code>Host: identi.ca<\/code><br \/>\n<code>Accept-Encoding: identity<\/code><br \/>\n<code>authorization: Basic SECRET_HASH_CONSTRUCTED_BY_HTTPLIB2 \u2463<\/code><br \/>\n<code>user-agent: Python-httplib2\/$Rev: 259 $<\/code><br \/>\n<code>'<\/code><br \/>\n<code>reply: 'HTTP\/1.1 200 OK' \u2464<\/code><br \/>\n<code>&gt;&gt;&gt; resp.status<\/code><br \/>\n<code>200<\/code><\/p>\n<ol>\n<li>\u5220\u9664\u8fd9\u6761\u72b6\u6001\u6d88\u606f\u3002<\/li>\n<li>\u672a\u6388\u6743\uff0c\u4e0d\u80fd\u5220\u9664\u3002<\/li>\n<li>\u518d\u6b21\u53d1\u9001\u8bf7\u6c42\u3002<\/li>\n<li>\u6388\u6743\u4fe1\u606f\uff0c\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/li>\n<li>\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4ecb\u7ecd \uff081\uff09HTTP WEB SERVER\u5c31\u662f\u548c\u8fdc\u7a0b\u670d\u52a1\u5668\u4ea4\u6362\u6570\u636e\uff0c\u9664\u4e86HTTP\u64cd\u4f5c\u5916\u4ec0\u4e48\u90fd\u4e0d\u7528\u3002\u4f7f\u7528http GET\u5f97\u5230\u6570\u636e\uff0c\u4f7f\u7528http POST\u53d1\u9001\u6570&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=234\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-dive-into-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=234"}],"version-history":[{"count":17,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":251,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/234\/revisions\/251"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}