E
E
Elios2015-06-30 10:38:48
Python
Elios, 2015-06-30 10:38:48

Python how to handle exception?

Tell me how to handle the exception

import requests
from proxy_list import runproxy

def check():
  httproxy = {"http": "http://" + runproxy()}
  request_handler = requests.get('http://www.yandex.ru', proxies=httproxy)
try:
  check()
except Exception, e:
  check()

in this code, runproxy() returns a random proxy from the checked proxy list, but since proxies tend to die all the time, I would like to add another level of verification. if any request fails, you need to call runproxy() to get another proxy and make the request again, and continue this until the request passes or until a certain number of attempts

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
bobrovskyserg, 2015-06-30
@strelov1

while True:
    try:
        check()
    except Exception, e:
        continue
    break

A
Arseny Kravchenko, 2015-06-30
@Arseny_Info

def check(): 
    httproxy = {"http": "http://" + runproxy()}
    result = requests.get('http://www.yandex.ru', proxies=httproxy)
    if status_code == 200:
        return httproxy

good_proxy = None
while not good_proxy:
    good_proxy = check()

A
Alexander Pinkevich, 2015-06-30
@pinkevich

if request_handler.status_code != 200:...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question