13

I'm trying to connect to https://apis.digital.gob.cl/fl/feriados/2020, but I get an requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',)) error on a script that works perfectly with other URLs.

The code:

import requests

response = requests.get('https://apis.digital.gob.cl/fl/feriados/2020')
print(response.status_code)
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Marco M.
  • 135
  • 1
  • 1
  • 4

2 Answers2

14

The issue is that the website filters out requests without a proper User-Agent, so just use a random one from MDN:

requests.get("https://apis.digital.gob.cl/fl/feriados/2020", headers={
"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
})
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • Thanks for the augmented user agent string! A simple agent string like `{'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0'}` doesn't get accepted by some websites. – edison23 Feb 17 '23 at 09:33
1

It might be due to idle timeout. Overriding default socket options can help

import socket
from urllib3.connection import HTTPConnection

HTTPConnection.default_socket_options = (
    HTTPConnection.default_socket_options + [
        (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
        (socket.SOL_TCP, socket.TCP_KEEPIDLE, 45),
        (socket.SOL_TCP, socket.TCP_KEEPINTVL, 10),
        (socket.SOL_TCP, socket.TCP_KEEPCNT, 6)
    ]
)
hardy_sandy
  • 361
  • 4
  • 6
  • 13