From d5c47ccb4a425d1b982888101273651b2d4db38d Mon Sep 17 00:00:00 2001 From: Marcin Krol Date: Sun, 2 May 2021 14:10:23 +0200 Subject: [PATCH] - use builtin python3 urllib instead of urllib2 --- TLD_Builder/file_sender.py | 11 +++++------ TLD_Builder/request_fetcher.py | 9 ++++----- TLD_Builder/rpm_builder.py | 17 ++++++++--------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/TLD_Builder/file_sender.py b/TLD_Builder/file_sender.py index 18ee8d8..0560458 100644 --- a/TLD_Builder/file_sender.py +++ b/TLD_Builder/file_sender.py @@ -8,8 +8,7 @@ import time import shutil import sys import traceback -import urllib2 - +import urllib from config import config, init_conf import mailer import path @@ -94,11 +93,11 @@ def post_file(src, url): global problems try: f = open(src, 'r') - data = f.read() + data = f.read().encode('utf-8') f.close() - req = urllib2.Request(url, data) - req.add_header('X-Filename', os.path.basename(src)) - f = urllib2.urlopen(req) + headers = { 'X-Filename' : os.path.basename(src) } + req = urllib.request.Request(url, data=data, headers=headers) + f = urllib.request.urlopen(req) f.close() except Exception as e: problems[src] = e diff --git a/TLD_Builder/request_fetcher.py b/TLD_Builder/request_fetcher.py index 25b130f..226e4ee 100644 --- a/TLD_Builder/request_fetcher.py +++ b/TLD_Builder/request_fetcher.py @@ -4,7 +4,6 @@ import string import signal import os import urllib -import urllib2 import sys from io import StringIO import gzip @@ -42,8 +41,8 @@ def has_new(control_url): signal.alarm(300) try: headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' } - req = urllib2.Request(url=control_url + "/max_req_no", headers=headers) - f = urllib2.urlopen(req) + req = urllib.request.Request(url=control_url + "/max_req_no", headers=headers) + f = urllib.request.urlopen(req) count = int(f.readline().strip()) signal.alarm(0) except Exception as e: @@ -62,8 +61,8 @@ def fetch_queue(control_url): signal.alarm(300) try: headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' } - req = urllib2.Request(url=control_url + "/queue.gz", headers=headers) - f = urllib2.urlopen(req) + req = urllib.request.Request(url=control_url + "/queue.gz", headers=headers) + f = urllib.request.urlopen(req) signal.alarm(0) except Exception as e: signal.alarm(0) diff --git a/TLD_Builder/rpm_builder.py b/TLD_Builder/rpm_builder.py index e33a7e1..65ab8e2 100644 --- a/TLD_Builder/rpm_builder.py +++ b/TLD_Builder/rpm_builder.py @@ -9,7 +9,6 @@ import time import datetime import string import urllib -import urllib2 from config import config, init_conf from bqueue import B_Queue @@ -62,12 +61,12 @@ def check_skip_build(r, b): while not good: try: headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' } - req = urllib2.Request(url=src_url, headers=headers) - f = urllib2.urlopen(req) + req = urllib.request.Request(url=src_url, headers=headers) + f = urllib.request.urlopen(req) good = True - except urllib2.HTTPError as error: + except urllib.error.HTTPError as error: return False - except urllib2.URLError as error: + except urllib.error.URLError as error: # see errno.h try: errno = error.errno @@ -92,10 +91,10 @@ def fetch_src(r, b): while not good: try: headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' } - req = urllib2.Request(url=src_url, headers=headers) - f = urllib2.urlopen(req) + req = urllib.request.Request(url=src_url, headers=headers) + f = urllib.request.urlopen(req) good = True - except urllib2.HTTPError as error: + except urllib.error.HTTPError as error: # fail in a way where cron job will retry msg = "unable to fetch url %s, http code: %d" % (src_url, error.code) b.log_line(msg) @@ -107,7 +106,7 @@ def fetch_src(r, b): msg = "in queue for more than 6 hours, download failing" b.log_line(msg) return False - except urllib2.URLError as error: + except urllib.error.URLError as error: errno = 0 if isinstance(error.args[0], IOError): errno = error.args[0].errno -- 2.46.0