|
10 | 10 | import os |
11 | 11 | import json |
12 | 12 | import multiprocessing |
13 | | -import urllib2 |
14 | 13 | import socket |
15 | 14 | import random |
16 | 15 | import shutil |
17 | 16 | import hashlib |
18 | | -from urlparse import urlparse |
19 | 17 |
|
20 | 18 | try: |
21 | 19 | from pkg_resources import resource_filename |
|
24 | 22 |
|
25 | 23 |
|
26 | 24 | import logging |
27 | | -from urlparse import urlparse |
| 25 | +try: |
| 26 | + from urllib.parse import urlparse |
| 27 | + from urllib.parse import quote as urlquote |
| 28 | + from urllib.error import URLError |
| 29 | + from urllib.request import Request, urlopen |
| 30 | +except ImportError: # Python 2 |
| 31 | + from urlparse import urlparse |
| 32 | + from urllib2 import quote as urlquote |
| 33 | + from urllib2 import URLError, Request, urlopen |
28 | 34 |
|
29 | 35 | # Version information for user-agent |
30 | 36 | VERSION = "5.6.2" |
@@ -412,7 +418,7 @@ def download_http(source, destination, debug, payload): |
412 | 418 | del os.environ['http_proxy'] |
413 | 419 |
|
414 | 420 | # Quote the source URL, which may have weird, dangerous characters |
415 | | - quoted_source = urllib2.quote(source) |
| 421 | + quoted_source = urlquote(source) |
416 | 422 | if scitoken_contents: |
417 | 423 | bearer_auth = "-H \"Authorization: Bearer %s\"" % (scitoken_contents) |
418 | 424 | else: |
@@ -493,11 +499,11 @@ def _es_send(payload): |
493 | 499 | data=json.dumps(data) |
494 | 500 | try: |
495 | 501 | url = "http://uct2-collectd.mwt2.org:9951" |
496 | | - req = urllib2.Request(url, data=data, headers={'Content-Type': 'application/json'}) |
497 | | - f = urllib2.urlopen(req) |
| 502 | + req = Request(url, data=data, headers={'Content-Type': 'application/json'}) |
| 503 | + f = urlopen(req) |
498 | 504 | f.read() |
499 | 505 | f.close() |
500 | | - except urllib2.URLError as e: |
| 506 | + except URLError as e: |
501 | 507 | logging.warning("Error posting to ES: %s", str(e)) |
502 | 508 |
|
503 | 509 | p = multiprocessing.Process(target=_es_send, name="_es_send", args=(payload,)) |
@@ -691,7 +697,7 @@ def get_stashservers_caches(responselines): |
691 | 697 | for l in lists[0:-1]: |
692 | 698 | names = names + ',' + l.split('=')[0] |
693 | 699 | # skip leading comma |
694 | | - print((names[1:])) |
| 700 | + print(names[1:]) |
695 | 701 | sys.exit(0) |
696 | 702 |
|
697 | 703 | if cache_list_name == None: |
@@ -757,15 +763,15 @@ def get_best_stashcache(): |
757 | 763 | logging.debug("Querying %s", final_url) |
758 | 764 | try: |
759 | 765 | # Make the request |
760 | | - req = urllib2.Request(final_url, headers=headers) |
761 | | - response = urllib2.urlopen(req, timeout=10) |
| 766 | + req = Request(final_url, headers=headers) |
| 767 | + response = urlopen(req, timeout=10) |
762 | 768 | if response.getcode() == 200: |
763 | 769 | logging.debug("Got OK code 200 from %s", cur_site) |
764 | 770 | responselines = response.read().split('\n') |
765 | 771 | response.close() |
766 | 772 | break |
767 | 773 | response.close() |
768 | | - except urllib2.URLError as e: |
| 774 | + except URLError as e: |
769 | 775 | logging.debug("URL error: %s", str(e)) |
770 | 776 | except Exception as e: |
771 | 777 | logging.debug("Error: %s", str(e)) |
|
0 commit comments