Python urllib“ TimeoutError:[WinError 10060]连接尝试失败,因为一段时间后被连接方未正确响应”

时间:2019-09-05 18:06:56

标签: python urllib

我每60秒请求一个网站并从那里获取信息,我让我的代码以24/7运行,但是大约2小时后,我在下面得到了这个奇怪的错误代码。

我了解了这个问题,有些人说使用代理,所以我做了,有些人说添加重试所有这些都无济于事,我仍然遇到这个错误。

我的请求代码:

def get_bitolim(class_num):
    bitolim = []
    url=r'http://tikah.iscool.co.il/default.aspx'
    s=requests.Session()
    retry = Retry(connect=3, backoff_factor=0.5)
    adapter = HTTPAdapter(max_retries=retry)
    s.mount('http://', adapter)
    s.mount('https://', adapter)
    r=s.get(url)
    soup=bs(r.text,'html.parser')

    dataload={
        '__EVENTTARGET': 'dnn$ctr6707$TimeTableView$ClassesList',
        '__EVENTARGUMENT': '',
        '__LASTFOCUS': '',
        '__VIEWSTATE': soup.find('input',{'id':'__VIEWSTATE'}).attrs['value'],
        '__VIEWSTATEGENERATOR': soup.find('input',{'id':'__VIEWSTATEGENERATOR'}).attrs['value'],
        'dnn$ctr6707$TimeTableView$ClassesList': class_num,
        'dnn$ctr6707$TimeTableView$ControlId': '2',
        'ScrollTop':'' ,
        '__dnnVariable': ''
    }
    headers={
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9',
        'cache-control': 'max-age=0',
        'content-length': '3278',
        'Host': 'tikah.iscool.co.il',
        'Origin': 'http://tikah.iscool.co.il',
        'Referer': 'http://tikah.iscool.co.il/default.aspx',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
    }
    r=s.post(r.url,headers=headers,data=dataload)
    page = r.text
    page_soup = bs(page, "html.parser")
    containers = page_soup.findAll('tr', {'bgcolor' : '#ffffff'})
    for container in containers:
        bitolim.append(container.td.text.strip())
    return bitolim

错误代码:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "site-packages\urllib3\connection.py", line 160, in _new_conn
  File "site-packages\urllib3\util\connection.py", line 80, in create_connection
  File "site-packages\urllib3\util\connection.py", line 70, in create_connection
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 "site-packages\urllib3\connectionpool.py", line 603, in urlopen
  File "site-packages\urllib3\connectionpool.py", line 355, in _make_request
  File "http\client.py", line 1239, in request
  File "http\client.py", line 1285, in _send_request
  File "http\client.py", line 1234, in endheaders
  File "http\client.py", line 1026, in _send_output
  File "http\client.py", line 964, in send
  File "site-packages\urllib3\connection.py", line 183, in connect
  File "site-packages\urllib3\connection.py", line 169, in _new_conn
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x038433B0>: 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 "site-packages\requests\adapters.py", line 449, in send
  File "site-packages\urllib3\connectionpool.py", line 670, in urlopen
  File "site-packages\urllib3\connectionpool.py", line 670, in urlopen
  File "site-packages\urllib3\connectionpool.py", line 670, in urlopen
  File "site-packages\urllib3\connectionpool.py", line 641, in urlopen
  File "site-packages\urllib3\util\retry.py", line 399, in increment
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='tikah.iscool.co.il', port=80): Max retries exceeded with url: /default.aspx (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x038433B0>: 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 "threading.py", line 916, in _bootstrap_inner
  File "threading.py", line 864, in run
  File "tikahServer.py", line 227, in server
  File "tikahServer.py", line 407, in get_bitolim
  File "site-packages\requests\sessions.py", line 546, in get
  File "site-packages\requests\sessions.py", line 533, in request
  File "site-packages\requests\sessions.py", line 646, in send
  File "site-packages\requests\adapters.py", line 516, in send
requests.exceptions.ConnectionError: HTTPConnectionPool(host='tikah.iscool.co.il', port=80): Max retries exceeded with url: /default.aspx (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x038433B0>: 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',))

0 个答案:

没有答案