X-Git-Url: https://git.tld-linux.org/?a=blobdiff_plain;f=TLD_Builder%2Frequest_fetcher.py;h=83229ae475b3c20eba57d4f2a2cc8d9b3a1f8c0c;hb=HEAD;hp=6122612142a2b31bac411ee22b14e68da0e2d57c;hpb=92949e17161a53bfb4a60099e22b58ad63abce4c;p=tld-builder.git diff --git a/TLD_Builder/request_fetcher.py b/TLD_Builder/request_fetcher.py index 6122612..83229ae 100644 --- a/TLD_Builder/request_fetcher.py +++ b/TLD_Builder/request_fetcher.py @@ -3,20 +3,16 @@ import string import signal import os -import urllib -import urllib2 +import urllib.request import sys -if sys.version_info[0] == 2: - import StringIO -else: - from io import StringIO +from io import StringIO, BytesIO import gzip - import path import log import status import lock import util +import shutil import gpg import request import loop @@ -29,13 +25,13 @@ from config import config, init_conf last_count = 0 def alarmalarm(signum, frame): - raise IOError, 'TCP connection hung' + raise IOError('TCP connection hung') def has_new(control_url): global last_count cnt_f = open(path.last_req_no_file) try: - last_count = int(string.strip(cnt_f.readline())) + last_count = int(cnt_f.readline().strip()) except ValueError as e: last_count = 0 @@ -46,9 +42,9 @@ 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) - count = int(string.strip(f.readline())) + 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: signal.alarm(0) @@ -66,15 +62,15 @@ 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) log.error("can't fetch %s: %s" % (control_url + "/queue.gz", e)) sys.exit(1) - sio = StringIO.StringIO() - util.sendfile(f, sio) + sio = BytesIO() + shutil.copyfileobj(f, sio) f.close() sio.seek(0) f = gzip.GzipFile(fileobj = sio)