How to catch exception MaxRetryError? - python

I have a list of proxy addresses for which I want to connect the telegrams-bot.
Proxies can be blocked over time or simply not work, this can be seen by the MaxRetryError error.
But I can't catch an error. I have an error in logs.
I want to catch an error and switch to another proxy server.
from telegram.ext import Updater
REQUEST_KWARGS = {
'proxy_url': proxy_url,
'urllib3_proxy_kwargs': {
'retries': 0
}
}
updater = Updater(token=TELEGRAM_TOKEN, request_kwargs=REQUEST_KWARGS)
queue = updater.start_polling(bootstrap_retries=0)
.....
raise MaxRetryError(_pool, url, error or ResponseError(cause))
telegram.vendor.ptb_urllib3.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot877422445:AAEGBD0D9stJKMF6PvCClChx8MNMGX-vLEY/deleteWebhook (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x11572df98>: Failed to establish a new connection: [Errno 61] Connection refused')))
......
telegram.error.NetworkError: urllib3 HTTPError HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot877422445:AAEGBD0D9stJKMF6PvCClChx8MNMGX-vLEY/deleteWebhook (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x11572df98>: Failed to establish a new connection: [Errno 61] Connection refused')))
2019-07-26 16:07:38,553 - telegram.ext.dispatcher - CRITICAL - stopping due to exception in another thread

This way ?
try:
updater = Updater(token=TELEGRAM_TOKEN, request_kwargs=REQUEST_KWARGS)
queue = updater.start_polling(bootstrap_retries=0)
except MaxRetryError:
#doSomething

Related

How to reopen selenium browser in a loop: Python

I'm trying to close the browser and reopen it after a loop
class Bot():
driver = webdriver.Chrome(options=chrome_options)
def __init__(self):
self.openyoutube
self.quitbrowser()
def openyoutube(self):
self.driver.get('https://www.youtube.com')
sleep(5)
def quitbrowser(self):
self.driver.quit()
def main():
while True:
my_bot = Bot()
sleep(15)
if __name__ == '__main__':
main()
But once it tries to start all over, it throws this error:
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost',
port=64578): Max retries exceeded with url:
/session/f7fcdfe14c3e2c75d530b3cbf70348d2/url (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7fb6d71f8250>: Failed to establish a new connection: [Errno 61]
Connection refused'))
raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=64578): Max retries exceeded with url: /session/f7fcdfe14c3e2c75d530b3cbf70348d2/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb6d71f8250>: Failed to establish a new connection: [Errno 61] Connection refused'))
Generally after this error we reinitialise the driver which you can do by using a try catch for Maxretryerror.
driver = webdriver.Chrome(options=chrome_options)

Requests giving errors while using HTTP proxies

So, I was sending a request using the requests library in Python 3.9.1. The problem is, when I tried to use an HTTP proxy it gave me this error:
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002B08D6BC9A0>: 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')))
This my code, I would appreciate any help.:
import requests
for proxy in open('proxies.txt','r').readlines():
proxies = {
'http': f'http://{proxy}',
'https': f'http://{proxy}'
}
e = requests.get('https://google.com/robots.txt',proxies=proxies)
open('uwu.txt','a').write(e.text)
print(e.text)
I am pretty sure it is not problem with my proxies as they are really good private proxies with 100 gigs of bandwidth. (from zenum.io).

Python get and post requests fails with Connection and GetAddrinfo

import requests
r = requests.get('http://http2bin.org/get')
I am getting below errors:
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError:
HTTPConnectionPool(host='http2bin.org', port=80): Max retries exceeded
with url: /get (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x031CAB08>: Failed to establish a new connection: [Errno 11004]
getaddrinfo failed'))
What could be the reason? Is it related to Proxy ?
It's a website problem.Try it with another website surely it would work.

Requests library get method error in python3.6

When I try to use the requests lib get method I get an error:
(using tkinter)
The error comes from this line:
var.set(get('https://api.ipify.org').text)
the error message says :
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.ipify.org', port=443): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused',))

ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url:

I am getting the Connection error while trying to scrap data from website.
Below is the code I am using.
import requests
import bs4
res=requests.get('https://www.google.com/')
type(res)
soup=bs4.BeautifulSoup(res.text,'lxml')
type(soup)
print(type(soup))
print(soup.prettify())
soup1=soup.select('title')
Below is the complete error message.
raise ConnectionError(e, request=request)
ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))

Categories