'TimeoutError: [WinError 10060] - Twitter API v.2.0

I am extracting tweets using the code below and everything works fine for about 20 minutes. However, suddenly I encounter the following error TimeoutError: [WinError 10060].

Is the error caused by a fault on my part? My PC is connected via LAN and the internet connection is stable throughout the whole process of extracting tweets from the Twitter API v2.0.

Can the error be circumvented using an exception?

# code extract tweets from 138 queries, start and end dates and saves each to a unique csv file
# named after the day of Tuckers show.
for suffixes_1, suffixes_2, idx, name in zip(start_time_control, end_time_control, index_list, search_query_control):
    for s1, s2 in zip(suffixes_1, suffixes_2):

        # Inputs
        count = 0  # Counting tweets per time period/journalist
        max_count = 20  # Max tweets per time period/journalist
        flag = True
        next_token = None

        # create csv files named after date
        csvFile = open("control_period" + "_" + str(idx) + ".csv", "a", newline="", encoding='utf-8')
        csvWriter = csv.writer(csvFile)

        # create headers for the four variables: author_id, created_at, id, and tweet
        csvWriter.writerow(
            ['author_id', 'created_at', 'id', 'tweet'])
        csvFile.close()

        # create url for tweet extraction based on for loop:
        # loop over 147 queries, start and end dates
        url = create_url(name, s1, s2, max_results)
        json_response = connect_to_endpoint(url[0], headers, url[1], next_token)
        result_count = json_response['meta']['result_count']

        # Check if flag is true
        while flag:

            # Check if max_count reached
            if count >= max_count:
                break
            print("-------------------")
            print("Token: ", next_token)

            if 'next_token' in json_response['meta']:
                #  Save the token to use for next call
                next_token = json_response['meta']['next_token']
                print("Next Token: ", next_token)
                if result_count is not None and result_count > 0 and next_token is not None:
                    print("Start Date: ", s1)
                    append_to_csv(json_response, "control_period" + "_" + str(idx) + ".csv")
                    count += result_count
                    total_tweets += result_count
                    print("Total # of Tweets added: ", total_tweets)
                    print("-------------------")
                    sleep(5)
                    # If no next token exists
            else:
                if result_count is not None and result_count > 0:
                    print("-------------------")
                    print("Start Date: ", s1)
                    append_to_csv(json_response, "control_period" + "_" + str(idx) + ".csv")
                    count += result_count
                    total_tweets += result_count
                    print("Total # of Tweets added: ", total_tweets)
                    print("-------------------")
                    sleep(5)

                    # Since this is the final request, turn flag to false to move to the next time period.
                    flag = False
                    next_token = None
                    sleep(5)
print("Total number of results: ", total_tweets)

The entire error message

Traceback (most recent call last):
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
    raise err
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\connectionpool.py", line 1040, in _validate_conn
    conn.connect()
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\connection.py", line 358, in connect
    conn = self._new_conn()
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x00727F70>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\requests\adapters.py", line 440, in send
    resp = conn.urlopen(
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\urllib3\util\retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.twitter.com', port=443): Max retries exceeded with url: /2/tweets/search/all?query=%28%40JohnKingCNN+OR+%22John+King%22%29+-is%3Aretweet&start_time=2022-01-25T00%3A00%3A00.000Z&end_time=2022-01-25T23%3A59%3A59.000Z&max_results=20&expansions=author_id&tweet.fields=id%2Ctext%2Cauthor_id%2Ccreated_at (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00727F70>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Marco Liedecke\AppData\Local\Programs\Python\Python38-32\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 25, in <module>
  File "<input>", line 33, in connect_to_endpoint
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Marco Liedecke\PycharmProjects\fuzzy_string_match_code\venv\lib\site-packages\requests\adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.twitter.com', port=443): Max retries exceeded with url: /2/tweets/search/all?query=%28%40JohnKingCNN+OR+%22John+King%22%29+-is%3Aretweet&start_time=2022-01-25T00%3A00%3A00.000Z&end_time=2022-01-25T23%3A59%3A59.000Z&max_results=20&expansions=author_id&tweet.fields=id%2Ctext%2Cauthor_id%2Ccreated_at (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00727F70>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source