requests測試代理IP是否可用

def test_ip():

? ? test_url = "https://www.baidu.com"

? ? headers = {

? ? ? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'

? ? }

? ? while True:

? ? ? ? temp_ip, temp_port = getIp()

? ? ? ? print(f"開始測試 {temp_ip}:{temp_port}")

? ? ? ? proxies = {

? ? ? ? ? ? "http": f"http://{temp_ip}:{temp_port}/",

? ? ? ? ? ? "https": f"http://{temp_ip}:{temp_port}/"

? ? ? ? }

? ? ? ? try:

? ? ? ? ? ? result = requests.get(url=test_url, headers=headers, proxies=proxies, timeout=3)

? ? ? ? ? ? if result.status_code == 200:

? ? ? ? ? ? ? ? print(f"{temp_ip}:{temp_port},可通baidu")

? ? ? ? ? ? ? ? print(result.text)

? ? ? ? ? ? ? ? return temp_ip, temp_port

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? print(f"{temp_ip}:{temp_port},不通baidu")

? ? ? ? ? ? ? ? continue

? ? ? ? except Exception as exception:

? ? ? ? ? ? print(exception)

? ? ? ? ? ? continue

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 大師兄的Python學(xué)習(xí)筆記(十九): Python與(XML和JSON)師兄的Python學(xué)習(xí)筆記(二十一): ...
    superkmi閱讀 2,313評論 0 4
  • 使用requests可以模擬瀏覽器的請求,比起之前用到的urllib,requests模塊的api更加便捷(本質(zhì)就...
    SkTj閱讀 3,585評論 0 0
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,834評論 28 54
  • 人工智能是什么?什么是人工智能?人工智能是未來發(fā)展的必然趨勢嗎?以后人工智能技術(shù)真的能達(dá)到電影里機(jī)器人的智能水平嗎...
    ZLLZ閱讀 4,098評論 0 5
  • 首先介紹下自己的背景: 我11年左右入市到現(xiàn)在,也差不多有4年時(shí)間,看過一些關(guān)于股票投資的書籍,對于巴菲特等股神的...
    瞎投資閱讀 5,938評論 3 8

友情鏈接更多精彩內(nèi)容