0

hello my whole code has troubles because of this : i want to make a loop as, if a post request doesn't work it does another with the next proxy which is the file list, etc (do this until i get no errors). But i struggle converting this try and except example into a loop

try:


  requests.post(url, headers={"Accept": "application/json"}, proxies =proxy1).json()
except requests.exceptions.ConnectionError or requests.exceptions.ConnectTimeout or requests.exceptions.ProxyError or urllib3.exceptions.MaxRetryError or urllib3.exceptions.NewConnectionError or OSError:
    #takeAnotherProxy and do another post request
     with open ("proxielist.txt",'r') as test:
        linee = test.readlines()
        proxy = linee[g].rstrip()
        proxy1 = {
            "http": 'http://' + proxy,
            "https": 'http://' + proxy}
            try:
                requests.post(url, headers={"Accept": "application/json"}, proxies =proxy1).json()
            except requests.exceptions.ConnectionError or requests.exceptions.ConnectTimeout or requests.exceptions.ProxyError or urllib3.exceptions.MaxRetryError or urllib3.exceptions.NewConnectionError or OSError
  • You want `, ` between the exception types, not `or`. – Karl Knechtel May 02 '20 at 16:14
  • it gives me invalid syntax – Mehditation May 02 '20 at 16:23
  • Pardon, parentheses are required too https://stackoverflow.com/questions/6470428/catch-multiple-exceptions-in-one-line-except-block but the `or` thing definitely does not work, for the same reason that it doesn't work for comparing to multiple values. – Karl Knechtel May 02 '20 at 16:31
  • can i make a loop like that : as soon it has no error the while loop ends? while (requests.exceptions.ConnectionError , requests.exceptions.ConnectTimeout , requests.exceptions.ProxyError , urllib3.exceptions.MaxRetryError, urllib3.exceptions.NewConnectionError, OSError): – Mehditation May 02 '20 at 16:41

0 Answers0