{"id":5736,"date":"2014-09-17T09:18:56","date_gmt":"2014-09-17T01:18:56","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=5736"},"modified":"2014-09-17T09:18:56","modified_gmt":"2014-09-17T01:18:56","slug":"%e8%ae%a9urllib2%e4%bd%bf%e7%94%a8%e8%87%aa%e5%ae%9a%e4%b9%89%e7%9a%84dns","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/5736.html","title":{"rendered":"\u8ba9urllib2\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684DNS"},"content":{"rendered":"<p>\u4eca\u5929\u7528urllib2\u53bb\u6293\u7f51\u9875\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u975e\u5e38\u6162\uff0c\u800c\u7528\u6d4f\u89c8\u5668\u6253\u5f00\u7f51\u9875\u5c31\u975e\u5e38\u5feb\uff0c\u6240\u4ee5\u6211\u6000\u7591urllib2\u5728\u8fdb\u884cDNS\u89e3\u6790\u8fd9\u4e00\u6b65\u9aa4\u6162\u4e86\uff0c\u56e0\u4e3a\u6d4f\u89c8\u5668\u5b83\u81ea\u5df1\u4f1a\u5bf9DNS\u8fdb\u884c\u7f13\u5b58\u7684\u3002<\/p>\n<p>\u5728\u4e0d\u6539\u672c\u5730hosts\u6587\u4ef6\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7528\u4ee3\u7801\u8bbe\u7f6e\u6765\u6539\u53d8urllib2\u7684DNS\u89e3\u6790\u903b\u8f91\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport urllib2\r\nimport httplib\r\nimport socket\r\n\r\ndef MyResolver(host):\r\n  if host == 'news.bbc.co.uk':\r\n    return '66.102.9.104' # Google IP\r\n  else:\r\n    return host\r\n\r\nclass MyHTTPConnection(httplib.HTTPConnection):\r\n  def connect(self):\r\n    self.sock = socket.create_connection((MyResolver(self.host),self.port),self.timeout)\r\nclass MyHTTPSConnection(httplib.HTTPSConnection):\r\n  def connect(self):\r\n    sock = socket.create_connection((MyResolver(self.host), self.port), self.timeout)\r\n    self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)\r\n\r\nclass MyHTTPHandler(urllib2.HTTPHandler):\r\n  def http_open(self,req):\r\n    return self.do_open(MyHTTPConnection,req)\r\n\r\nclass MyHTTPSHandler(urllib2.HTTPSHandler):\r\n  def https_open(self,req):\r\n    return self.do_open(MyHTTPSConnection,req)\r\n\r\nopener = urllib2.build_opener(MyHTTPHandler,MyHTTPSHandler)\r\nurllib2.install_opener(opener)\r\n\r\nf = urllib2.urlopen('http:\/\/news.bbc.co.uk')\r\ndata = f.read()\r\nfrom lxml import etree\r\ndoc = etree.HTML(data)\r\n\r\n&gt;&gt;&gt; print doc.xpath('\/\/title\/text()')\r\n&#x5B;'Google']\r\n<\/pre>\n<p>\u6765\u6e90\uff1ahttp:\/\/stackoverflow.com\/questions\/2236498\/tell-urllib2-to-use-custom-dns<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u7528urllib2\u53bb\u6293\u7f51\u9875\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u975e\u5e38\u6162\uff0c\u800c\u7528\u6d4f\u89c8\u5668\u6253\u5f00\u7f51\u9875\u5c31\u975e\u5e38\u5feb\uff0c\u6240\u4ee5\u6211\u6000\u7591urllib2\u5728\u8fdb\u884cDN [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-5736","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=5736"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5736\/revisions"}],"predecessor-version":[{"id":5737,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5736\/revisions\/5737"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=5736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=5736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=5736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}