@@ -36,6 +36,7 @@ def json(self):
3636def request (
3737 method , url , data = None , json = None , headers = {}, stream = None , parse_headers = True , auth = None
3838):
39+ redirect = None # redirection url, None means no redirection
3940 chunked = data and is_chunked_data (data )
4041
4142 if auth is not None :
@@ -119,7 +120,10 @@ def request(
119120 if b"chunked" in l :
120121 raise ValueError ("Unsupported " + l .decode ())
121122 elif l .startswith (b"Location:" ) and not 200 <= status <= 299 :
122- raise NotImplementedError ("Redirects not yet supported" )
123+ if status in [301 , 302 , 303 , 307 , 308 ]:
124+ redirect = l [10 :- 2 ].decode ()
125+ else :
126+ raise NotImplementedError ("Redirect %d not yet supported" % status )
123127 if parse_headers is False :
124128 pass
125129 elif parse_headers is True :
@@ -132,12 +136,19 @@ def request(
132136 s .close ()
133137 raise
134138
135- resp = Response (s )
136- resp .status_code = status
137- resp .reason = reason
138- if resp_d is not None :
139- resp .headers = resp_d
140- return resp
139+ if redirect :
140+ s .close ()
141+ if status in [301 , 302 , 303 ]:
142+ return request ("GET" , redirect , None , None , headers , stream )
143+ else :
144+ return request (method , redirect , data , json , headers , stream )
145+ else :
146+ resp = Response (s )
147+ resp .status_code = status
148+ resp .reason = reason
149+ if resp_d is not None :
150+ resp .headers = resp_d
151+ return resp
141152
142153
143154def head (url , ** kw ):
0 commit comments