Python Forum
Running A Loop Until You See A Particular Result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running A Loop Until You See A Particular Result
#7
(Sep-01-2021, 02:20 PM)DeaD_EyE Wrote: Hi DeaD_EyE,

Sorry for such a delay in replying.

In more testing of the original suggestion, I found that I was still having an issue (my oversight- but still very grateful he took time out to help me)

After some hours I figured out it was something to do with error handling, but didn't know how to apply that. I looked up lots of various posts but I couldn't make it work for myself.

Then came across your solution here that you posted...

This works perfectly- thank you so much. Big Grin

There's no way I'd would have known how to apply it this elegantly- with error handling messaging included! I will try and study this more and understand what some of it's inclusions mean- like file=sys.stderr for example.

Thanks again and have a great weekend.



Some improvements + error corrections + info about urls..

import random
import sys
import time

import requests

# Take the right protocol
# "testurl.com" is not a valid URL
# "http://testurl.com" is valid
url = "https://python-forum.io/thread-34789.html"

# set headers
# this was missing in the code example and this was causing the
# NameError
headers = {}

# Proxies must also start with http:// or https://
proxies = [
    "http://173.68.59.131:3128",
    "http://64.124.38.139:8080",
    "http://69.197.181.202:3128",
]
random.shuffle(proxies)


result = None

for proxy in proxies:
    try:
        response = requests.get(
            url, headers=headers, proxies={"https": proxy}, timeout=3
        )
    except (requests.ReadTimeout, requests.ConnectionError):
        print("Got timeout", file=sys.stderr)
        continue
    except Exception as e:
        print("Contact the programer", repr(e), file=sys.stderr)
    else:
        print(response.status_code, file=sys.stderr)
        # be a good shell citizen
        # don't print debugging data to stdout

        if response.status_code == 200:
            result = response.text
            # break out of loop if result was found
            break


if result is None:
    print("No success", file=sys.stderr)
else:
    time.sleep(2)
    print(result)
Reply


Messages In This Thread
RE: Running A Loop Until You See A Particular Result - by knight2000 - Sep-04-2021, 08:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help RuntimeError: no running event loop marpaslight 5 3,750 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,513 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  bleak library RuntimeError: This event loop is already running alice93 3 4,106 Sep-30-2021, 08:06 AM
Last Post: alice93
  loop running indefinitely shantanu97 6 2,591 Sep-29-2021, 08:03 PM
Last Post: deanhystad
  Running loop at specific frequency mdsousa 3 5,948 Apr-21-2021, 11:22 AM
Last Post: jefsummers
  Noob Alert! Wrong result using loop and if statemnent GJG 7 2,887 Dec-19-2020, 05:18 PM
Last Post: buran
  RuntimeError: This event loop is already running newbie2019 2 6,963 Sep-30-2020, 06:59 PM
Last Post: forest44
  Running function from parent module which has a loop in it. ta2909i 1 2,692 Nov-18-2019, 07:04 PM
Last Post: Gribouillis
  How to add coroutine to a running event loop? AlekseyPython 1 8,167 Mar-21-2019, 06:04 PM
Last Post: nilamo
  action on MQTT while long loop is running runboy 4 6,108 Oct-05-2018, 11:57 PM
Last Post: runboy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020