proxy = None
def get(self, bookmark, url, accept_charset=False):
- try:
- r = requests.Session().get(
- url, timeout=self.timeout, allow_redirects=False)
- except requests.RequestException as e:
- error = str(e)
+ error, r = request_get(url, self.timeout, None)
+ if error is not None:
self.log(' Error: %s' % error)
if self.proxy:
- error = None
self.log(' Retrying with the proxy...')
- try:
- r = requests.get(
- url, timeout=self.timeout, allow_redirects=False,
- proxies={'http': self.proxy, 'https': self.proxy})
- except requests.RequestException as e:
- error = str(e)
- self.log(' Proxy error: %s' % error)
- if error is not None:
- return error, None, None, None, None
+ error, r = request_get(url, self.timeout, self.proxy)
+ if error is not None:
+ if self.proxy:
+ self.log(' Proxy error: %s' % error)
+ return error, None, None, None, None
if r.is_redirect:
return None, r.status_code, r.next.url, None, None
return None, None, None, r.headers, r.content
def get_ftp_welcome(self):
return '' # Alas, requests_ftp doesn't store welcome message
+
+
+def request_get(url, timeout, proxy):
+ if proxy:
+ proxies = {'http': proxy, 'https': proxy}
+ else:
+ proxies = None
+
+ try:
+ r = requests.Session().get(
+ url, timeout=timeout, allow_redirects=False, proxies=proxies)
+ except requests.RequestException as e:
+ return str(e), None
+ else:
+ return None, r