follow redirects, fix #193
This commit is contained in:
parent
00bb42e831
commit
3cd475ec31
@ -4,7 +4,7 @@ Changelog for Isso
|
|||||||
0.10.3 (unreleased)
|
0.10.3 (unreleased)
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
- Nothing changed yet.
|
- follow redirects, #193
|
||||||
|
|
||||||
|
|
||||||
0.10.2 (2016-02-21)
|
0.10.2 (2016-02-21)
|
||||||
|
@ -4,12 +4,16 @@ import socket
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
import httplib
|
import httplib
|
||||||
|
from urlparse import urlparse
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import http.client as httplib
|
import http.client as httplib
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from isso import dist
|
from isso import dist
|
||||||
from isso.wsgi import urlsplit
|
from isso.wsgi import urlsplit
|
||||||
|
|
||||||
|
MAX_RETRY_COUNT = 3
|
||||||
|
|
||||||
|
|
||||||
class curl(object):
|
class curl(object):
|
||||||
"""Easy to use wrapper around :module:`httplib`. Use as context-manager
|
"""Easy to use wrapper around :module:`httplib`. Use as context-manager
|
||||||
@ -23,7 +27,7 @@ class curl(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"User-Agent": "Isso/{0} (+http://posativ.org/isso)".format(dist.version)
|
"User-Agent": "Isso/{0} (+https://posativ.org/isso)".format(dist.version)
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, method, host, path, timeout=3):
|
def __init__(self, method, host, path, timeout=3):
|
||||||
@ -33,19 +37,27 @@ class curl(object):
|
|||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
|
||||||
host, port, ssl = urlsplit(self.host)
|
host, port, ssl = urlsplit(self.host)
|
||||||
http = httplib.HTTPSConnection if ssl else httplib.HTTPConnection
|
http = httplib.HTTPSConnection if ssl else httplib.HTTPConnection
|
||||||
|
|
||||||
|
for _ in range(MAX_RETRY_COUNT):
|
||||||
self.con = http(host, port, timeout=self.timeout)
|
self.con = http(host, port, timeout=self.timeout)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.con.request(self.method, self.path, headers=self.headers)
|
self.con.request(self.method, self.path, headers=self.headers)
|
||||||
except (httplib.HTTPException, socket.error):
|
except (httplib.HTTPException, socket.error) as e:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return self.con.getresponse()
|
resp = self.con.getresponse()
|
||||||
|
if resp.status == 301:
|
||||||
|
location = resp.getheader('Location')
|
||||||
|
if location:
|
||||||
|
self.con.close()
|
||||||
|
self.path = urlparse(location).path
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return resp
|
||||||
except (httplib.HTTPException, socket.timeout, socket.error):
|
except (httplib.HTTPException, socket.timeout, socket.error):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user