Skip to content

Commit d49fb9f

Browse files
committed
Handle urllib moves
1 parent ed8395d commit d49fb9f

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

stashcp/__init__.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
import os
1111
import json
1212
import multiprocessing
13-
import urllib2
1413
import socket
1514
import random
1615
import shutil
1716
import hashlib
18-
from urlparse import urlparse
1917

2018
try:
2119
from pkg_resources import resource_filename
@@ -24,7 +22,15 @@
2422

2523

2624
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
2834

2935
# Version information for user-agent
3036
VERSION = "5.6.2"
@@ -412,7 +418,7 @@ def download_http(source, destination, debug, payload):
412418
del os.environ['http_proxy']
413419

414420
# Quote the source URL, which may have weird, dangerous characters
415-
quoted_source = urllib2.quote(source)
421+
quoted_source = urlquote(source)
416422
if scitoken_contents:
417423
bearer_auth = "-H \"Authorization: Bearer %s\"" % (scitoken_contents)
418424
else:
@@ -493,11 +499,11 @@ def _es_send(payload):
493499
data=json.dumps(data)
494500
try:
495501
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)
498504
f.read()
499505
f.close()
500-
except urllib2.URLError as e:
506+
except URLError as e:
501507
logging.warning("Error posting to ES: %s", str(e))
502508

503509
p = multiprocessing.Process(target=_es_send, name="_es_send", args=(payload,))
@@ -691,7 +697,7 @@ def get_stashservers_caches(responselines):
691697
for l in lists[0:-1]:
692698
names = names + ',' + l.split('=')[0]
693699
# skip leading comma
694-
print((names[1:]))
700+
print(names[1:])
695701
sys.exit(0)
696702

697703
if cache_list_name == None:
@@ -757,15 +763,15 @@ def get_best_stashcache():
757763
logging.debug("Querying %s", final_url)
758764
try:
759765
# 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)
762768
if response.getcode() == 200:
763769
logging.debug("Got OK code 200 from %s", cur_site)
764770
responselines = response.read().split('\n')
765771
response.close()
766772
break
767773
response.close()
768-
except urllib2.URLError as e:
774+
except URLError as e:
769775
logging.debug("URL error: %s", str(e))
770776
except Exception as e:
771777
logging.debug("Error: %s", str(e))

0 commit comments

Comments
 (0)