{"id":1591,"date":"2022-02-21T15:02:33","date_gmt":"2022-02-21T15:02:33","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1591"},"modified":"2022-06-24T15:07:00","modified_gmt":"2022-06-24T15:07:00","slug":"python-requests-proxy","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2022\/02\/python-requests-proxy\/","title":{"rendered":"[python]requests\u4f7f\u7528proxy\u4ee3\u7406\u670d\u52a1\u5668"},"content":{"rendered":"

python\u4e2drequests\u6a21\u5757\uff0c\u652f\u6301\u8bbe\u7f6e\u4ee3\u7406\u6765\u8bbf\u95ee\u76ee\u6807\u7f51\u7ad9\u3002<\/p>\n

\u4e00\u3001\u4f7f\u7528HTTP\u4ee3\u7406<\/h2>\n
\n
import requests\n\nheaders = {\n    'user-agent': 'Mozilla\/0.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/67.0.3396.79 Safari\/537.36'\n}\n\nproxies = {\n  'http': 'http:\/\/172.28.9.46:10802',\n  'https': 'http:\/\/172.28.9.46:10802',\n  'https:\/\/www.getip.org': 'http:\/\/172.28.9.46:10802'\n}\n\nresponse = requests.get('https:\/\/www.getip.org', headers=headers ,proxies=proxies)\nprint(response.text)\n<\/code><\/pre>\n<\/div>\n

\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a
\n\u5982\u679c\u53ea\u914d\u7f6e
\n 'http': 'http:\/\/172.28.9.46:10802<\/a>',
\n\u90a3\u4e48\uff0c\u53ea\u6709\u8bbf\u95eehttp\u534f\u8bae\u7f51\u7ad9\u624d\u4f1a\u8d70\u4ee3\u7406\uff0c\u540c\u7406https\u4e5f\u662f\u5982\u6b64\u3002
\n\u5982\u679c\u53ea\u60f3\u8bbf\u95ee\u67d0\u4e9b\u4e2a\u522b\u7f51\u7ad9\u65f6\u8d70\u4ee3\u7406\uff0c\u7528\u5982\u4e0b\u5199\u6cd5
\n '
https:\/\/www.getip.org<\/a>': 'http:\/\/172.28.9.46:10802<\/a>'
\nKV\u952e\u503c\u5bf9\u4e2d\uff0cK\u662f\u8bbf\u95ee\u7684\u7ad9\u70b9\uff0cV\u662f\u4ee3\u7406\u670d\u52a1\u5668\u5730\u5740\u3002<\/p>\n

\u4e8c\u3001SOCKS5\u4ee3\u7406<\/h2>\n

\u9664\u4e86\u57fa\u672c\u7684 HTTP \u4ee3\u7406\u4e4b\u5916\uff0cRequests \u8fd8\u652f\u6301\u4f7f\u7528 SOCKS \u534f\u8bae\u7684\u4ee3\u7406\u3002
\n\u8fd9\u662f\u4e00\u9879\u53ef\u9009\u529f\u80fd\uff0c\u9700\u8981\u5728\u4f7f\u7528\u524d\u5b89\u88c5\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982\u4e0b\uff1a<\/p>\n

\n
pip install requests[socks] -i https:\/\/mirrors.aliyun.com\/pypi\/simple<\/code><\/pre>\n<\/div>\n

\u4ee3\u7801\u5982\u4e0b<\/p>\n

\n
import requests\n\nheaders = {\n    'user-agent': 'Mozilla\/0.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/67.0.3396.79 Safari\/537.36'\n}\n\nproxies = {\n  'http': 'socks5h:\/\/172.28.9.46:10801',\n  'https': 'socks5h:\/\/172.28.9.46:10801',\n}\n\nresponse = requests.get('https:\/\/www.getip.org', headers=headers ,proxies=proxies)\nprint(response.text)<\/code><\/pre>\n<\/div>\n

\u4e09\u3001DNS\u89e3\u6790\u7684\u95ee\u9898<\/h2>\n

HTTP\u3001HTTPS\u3001SOCKS5H\u90fd\u662f\u8fdc\u7a0b\u4ee3\u7406\u670d\u52a1\u5668\u6765\u8fdb\u884c\u57df\u540d\u89e3\u6790\u3002
\nSOCKS5\u662f\u672c\u5730\u8fdb\u884c\u57df\u540d\u89e3\u6790\uff0c\/etc\/hosts\u6587\u4ef6\u4f1a\u751f\u6548\u3002<\/p>\n

\u672c\u5730\u89e3\u6790\u548c\u8fdc\u7a0b\u89e3\u6790\u6709\u4ec0\u4e48\u533a\u522b\uff1f
\n\u4e00\u822c\u60c5\u51b5\u6ca1\u6709\u533a\u522b\uff0c\u4f46\u662f\u9047\u5230\u4e00\u4e9b\u88ab\u5899\u7684\u57df\u540d\uff0c\u4f7f\u7528\u672c\u5730\u89e3\u6790\u65e0\u6cd5\u8bbf\u95ee\uff0c\u53ea\u80fd\u4f7f\u7528\u8fdc\u7a0b\u89e3\u6790\u3002<\/p>\n

\u53c2\u8003\u8d44\u6599<\/h2>\n

https:\/\/requests.readthedocs.io\/en\/latest\/user\/advanced\/#proxies<\/a><\/p>\n

\u6765\u81ea\u4e3a\u77e5\u7b14\u8bb0(Wiz)<\/a><\/div>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » [python]requests\u4f7f\u7528proxy\u4ee3\u7406\u670d\u52a1\u5668<\/a><\/p>","protected":false},"excerpt":{"rendered":"

python\u4e2drequests\u6a21\u5757\uff0c\u652f\u6301\u8bbe\u7f6e\u4ee3\u7406\u6765\u8bbf\u95ee\u76ee\u6807\u7f51\u7ad9\u3002 \u4e00\u3001\u4f7f\u7528HTTP\u4ee3\u7406 import requests headers = { ‘user-agent’: ‘Mozilla\/0.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/67.0.3396.79 Safari\/537.36’ } proxies = { ‘http’: ‘http:\/\/172.28.9.46:10802’, ‘https’: ‘http:\/\/172.28.9.46:10802’, ‘https:\/\/www.getip.org’: ‘http:\/\/172.28.9.46:10802’ } response = requests.get(‘https:\/\/www.getip.org’, headers=headers ,proxies=proxies) print(response.text) \u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a \u5982\u679c\u53ea\u914d\u7f6e ‘http’: ‘http:\/\/172.28.9.46:10802’, \u90a3\u4e48\uff0c\u53ea\u6709\u8bbf\u95eehttp\u534f\u8bae\u7f51\u7ad9\u624d\u4f1a\u8d70\u4ee3\u7406\uff0c\u540c\u7406https\u4e5f\u662f\u5982\u6b64\u3002 \u5982\u679c\u53ea\u60f3\u8bbf\u95ee\u67d0\u4e9b\u4e2a\u522b\u7f51\u7ad9\u65f6\u8d70\u4ee3\u7406\uff0c\u7528\u5982\u4e0b\u5199\u6cd5 ‘https:\/\/www.getip.org’: ‘http:\/\/172.28.9.46:10802’ KV\u952e\u503c\u5bf9\u4e2d\uff0cK\u662f\u8bbf\u95ee\u7684\u7ad9\u70b9\uff0cV\u662f\u4ee3\u7406\u670d\u52a1\u5668\u5730\u5740\u3002 \u4e8c\u3001SOCKS5\u4ee3\u7406 \u9664\u4e86\u57fa\u672c\u7684 HTTP \u4ee3\u7406\u4e4b\u5916\uff0cRequests \u8fd8\u652f\u6301\u4f7f\u7528 SOCKS \u534f\u8bae\u7684\u4ee3\u7406\u3002 \u8fd9\u662f\u4e00\u9879\u53ef\u9009\u529f\u80fd\uff0c\u9700\u8981\u5728\u4f7f\u7528\u524d\u5b89\u88c5\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982\u4e0b\uff1a pip install […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[231,59,230],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1591"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1591"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1591\/revisions"}],"predecessor-version":[{"id":1592,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1591\/revisions\/1592"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}