** 验证函数 **
import requests
from bs4 import BeautifulSoup
def validate(proxies):
https_url = 'https://ip.cn'
http_url = 'http://ip111.cn/'
headers = {'User-Agent': 'curl/7.29.0'}
https_r = requests.get( https_url, headers=headers, proxies=proxies, timeout=10)
http_r = requests.get( http_url, headers=headers, proxies=proxies, timeout=10)
soup = BeautifulSoup( http_r.content, 'html.parser')
result = soup.find(class_='card-body').get_text().strip().split('''\n''')[0]
print(f"当前使用代理:{proxies.values()}")
print(f"访问 https 网站使用代理:{https_r.json()}")
print(f"访问 http 网站使用代理:{result}")
** 测试 **
Case 1
proxies = {
'http': '222.189.244.56:48304',
'https': '222.189.244.56:48304'
}
validate(proxies)
输出
当前使用代理:dict_values(['222.189.244.56:48304', '222.189.244.56:48304'])
访问 https 网站使用代理:{'ip': '222.189.244.56', 'country': '江苏省扬州市', 'city': '电信'}
访问 http 网站使用代理:222.189.244.56 China / Nanjing
结果: 访问两个网站均使用了代理
Case 2
proxies = {
'http': '222.189.244.56:48304'
}
validate(proxies)
输出
当前使用代理:dict_values(['222.189.244.56:48304'])
访问 https 网站使用代理:{'ip': '118.24.234.46', 'country': '重庆市', 'city': '腾讯'}
访问 http 网站使用代理:222.189.244.56 China / Nanjing
结果: 只有 http 请求使用了代理
Case 3
proxies = {
'https': '222.189.244.56:48304'
}
validate(proxies)
输出
当前使用代理:dict_values(['222.189.244.56:48304'])
访问 https 网站使用代理:{'ip': '222.189.244.56', 'country': '江苏省扬州市', 'city': '电信'}
访问 http 网站使用代理:118.24.234.46 China / Nanning
结果: 只有 https 请求使用了代理
通过抓包发现,协议不对应的情况下,根本不会发起对代理服务器的请求。这是为什么呢?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.