列出请求代理使用的协议 - Python3
List Protocol Used by Requests Proxies - Python3
我有一个功能可以通过使用 Python3 中的 requests 库向几个网站发送请求来检查代理是否处于活动状态。我正在尝试确定发出请求时可用的协议(http、https、socks),但不知道如何操作。
函数如下:
import requests
def is_prox(proxy_server):
proxyDict = {"http": proxy_server,
"https": proxy_server,
"socks": proxy_server}
test_site = {"http://www.google.com", "http://whatsmyip.org", "http://www.facebook.com"}
headers = {'user-agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)'}
for site in test_site:
try:
r = requests.get(site, headers=headers, proxies=proxyDict)
status = r.status_code
if status is 200:
print(status)
else:
pass
except Exception as e:
print(str(e))
if __name__ == '__main__':
is_prox("http://149.56.232.18:3128")
和输出:
$>python3 proxcheck.py
200
但我希望输出显示为:200 - http
或 200 - https
,或者正在使用的协议。
我试过 requests.utils.getproxies()
和 requests.utils.get_environ_proxies(url)
但这不是我想要的。
我知道这是一本字典,但我不知道如何列出返回的字典元素 200
。
谁能帮帮我?
根据 t.m.adam 的建议,我得到了我想要的结果。我没有让 requests 遍历代理,而是设置了一个 for 循环来遍历字典和 return return 200。
这是更新后的函数,代码固定:
import requests
def is_prox(proxy_server):
proxyDict = {"http": proxy_server,
"https": proxy_server,
"socks": proxy_server}
test_site = "http://api.ipify.org/?format=json"
headers = {'user-agent': 'Mozilla/'
'5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/'
'20091102 Firefox/'
'3.5.5 (.NET CLR 3.5.30729)'}
for proxy in proxyDict:
try:
r = requests.get(test_site, headers=headers, proxies=proxy)
status = r.status_code
if status is 200:
print(str(status) + " - " + str(proxy.upper())
except Exception as e:
pass
if __name__ == '__main__':
is_prox("http://149.56.232.18:3128")
其中 return 个:
200 - SOCKS
我有一个功能可以通过使用 Python3 中的 requests 库向几个网站发送请求来检查代理是否处于活动状态。我正在尝试确定发出请求时可用的协议(http、https、socks),但不知道如何操作。
函数如下:
import requests
def is_prox(proxy_server):
proxyDict = {"http": proxy_server,
"https": proxy_server,
"socks": proxy_server}
test_site = {"http://www.google.com", "http://whatsmyip.org", "http://www.facebook.com"}
headers = {'user-agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)'}
for site in test_site:
try:
r = requests.get(site, headers=headers, proxies=proxyDict)
status = r.status_code
if status is 200:
print(status)
else:
pass
except Exception as e:
print(str(e))
if __name__ == '__main__':
is_prox("http://149.56.232.18:3128")
和输出:
$>python3 proxcheck.py
200
但我希望输出显示为:200 - http
或 200 - https
,或者正在使用的协议。
我试过 requests.utils.getproxies()
和 requests.utils.get_environ_proxies(url)
但这不是我想要的。
我知道这是一本字典,但我不知道如何列出返回的字典元素 200
。
谁能帮帮我?
根据 t.m.adam 的建议,我得到了我想要的结果。我没有让 requests 遍历代理,而是设置了一个 for 循环来遍历字典和 return return 200。
这是更新后的函数,代码固定:
import requests
def is_prox(proxy_server):
proxyDict = {"http": proxy_server,
"https": proxy_server,
"socks": proxy_server}
test_site = "http://api.ipify.org/?format=json"
headers = {'user-agent': 'Mozilla/'
'5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/'
'20091102 Firefox/'
'3.5.5 (.NET CLR 3.5.30729)'}
for proxy in proxyDict:
try:
r = requests.get(test_site, headers=headers, proxies=proxy)
status = r.status_code
if status is 200:
print(str(status) + " - " + str(proxy.upper())
except Exception as e:
pass
if __name__ == '__main__':
is_prox("http://149.56.232.18:3128")
其中 return 个:
200 - SOCKS