I created an automatic transaction bot for Bitcoin and made a function to get the status of the parent order using the API called getparentorders of bitFlyer. It seems that the request has timed out, but I am not sure how to resolve this. Is it possible to prevent the program from stopping even if an error occurs, or to prevent timeout? Does the timeout never occur if the timeout value is increased?

Error message
requests.exceptions.ReadTimeout: HTTPSConnectionPool (host = 'api.bitflyer.jp', port = 443): Read timed out. (read timeout = 30)
The function in question
def position ():
    api_key = API_KEY
    api_secret = API_SECRET
    path_url = "/ v1/me/getparentorders? product_code = FX_BTC_JPY"
    method = "GET"
    timestamp = str (datetime.datetime.today ())

    message = timestamp + method + path_url
    #print (message)
    signature = hmac.new (bytearray (api_secret.encode ('utf-8')), message.encode ('utf-8'), digestmod = hashlib.sha256) .hexdigest ()
    #print (message.encode ('utf-8'))
    headers = {
        'ACCESS-KEY': api_key,
        'ACCESS-TIMESTAMP': timestamp,
        'ACCESS-SIGN': signature,
        'Content-Type': 'application/json'
    response = requests.get (base_url + path_url, headers = headers, timeout = 30)
    #pprint (response.json ())
    return response.json ()
  • Answer # 1

    The official documentation seems to give you arequests.exceptions.ConnectTimeoutexception on timeout, so you should receive it.

    import requests
    from time import sleep
    def connect_google (url):
        stand_by_sec = 5
            print (f "\ nurl: Connect to {url} \ t ...")
            r = requests.get (url, timeout = 0.001)
        except requests.exceptions.ConnectTimeout:
            # Wait 2 seconds and retry
            print (f "\ rTimed out ... \ nWaiting for reconnection ... Restart in {stand_by_sec} seconds", end = "", flush = True)
            sleep (stand_by_sec)
            connect_google (url = url)
            # Process on success
            return r
            # Clean up
    r = connect_google (url = "https://www.google.com")

    Would you like to define a helper function like this?

  • Answer # 2

    If you increase the timeout value, you might be able to cover when the response is somewhat slow, but just Even if this is set to 300, it will wait for 300 seconds. It doesn't seem to be a fundamental solution ...

    I think that it is necessary to incorporate proper exception handling and error handling, but if you do so, you can't do it without first thinking about how to continue processing when a timeout occurs.