Http utils now supports logging on our main logger torrentSearch. Changed indentation and added better error handling for requests.urlopen which is the function that fetches from the internet.
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
#!/usr/bin/env python3.6
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from urllib import parse, request
|
||||
from urllib.error import URLError
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger('torrentSearch')
|
||||
|
||||
def build_url(ssl, baseUrl, path, args_dict=[]):
|
||||
url_parts = list(parse.urlparse(baseUrl))
|
||||
@@ -24,15 +28,16 @@ def convert_query_to_percent_encoded_octets(input_query):
|
||||
return parse.quote(input_query)
|
||||
|
||||
def fetch_url(url):
|
||||
logger.debug('Fetching query: {}'.format(url))
|
||||
req = request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
|
||||
try:
|
||||
response = request.urlopen(req)
|
||||
response = request.urlopen(req, timeout=10)
|
||||
return response
|
||||
except URLError as e:
|
||||
if hasattr(e, 'reason'):
|
||||
logging.error('We failed to reach a server with request: %s' % req.full_url)
|
||||
logging.error('Reason: %s' % e.reason)
|
||||
logger.error('We failed to reach a server with request: %s' % req.full_url)
|
||||
logger.error('Reason: %s' % e.reason)
|
||||
elif hasattr(e, 'code'):
|
||||
logging.error('The server couldn\'t fulfill the request.')
|
||||
logging.error('Error code: ', e.code)
|
||||
else:
|
||||
return response
|
||||
logger.error('The server couldn\'t fulfill the request.')
|
||||
logger.error('Error code: ', e.code)
|
||||
sys.exit()
|
||||
|
||||
Reference in New Issue
Block a user