OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 # | 3 # |
4 # Copyright 2007 Google Inc. | 4 # Copyright 2007 Google Inc. |
5 # | 5 # |
6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
7 # you may not use this file except in compliance with the License. | 7 # you may not use this file except in compliance with the License. |
8 # You may obtain a copy of the License at | 8 # You may obtain a copy of the License at |
9 # | 9 # |
10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 extra_headers: Dict containing additional HTTP headers that should be | 417 extra_headers: Dict containing additional HTTP headers that should be |
418 included in the request (string header names mapped to their values), | 418 included in the request (string header names mapped to their values), |
419 or None to not include any additional headers. | 419 or None to not include any additional headers. |
420 kwargs: Any keyword arguments are converted into query string parameters. | 420 kwargs: Any keyword arguments are converted into query string parameters. |
421 | 421 |
422 Returns: | 422 Returns: |
423 The response body, as a string. | 423 The response body, as a string. |
424 """ | 424 """ |
425 # TODO: Don't require authentication. Let the server say | 425 # TODO: Don't require authentication. Let the server say |
426 # whether it is necessary. | 426 # whether it is necessary. |
427 if not self.authenticated: | 427 if not self.authenticated and self.auth_function: |
428 self._Authenticate() | 428 self._Authenticate() |
429 | 429 |
430 old_timeout = socket.getdefaulttimeout() | 430 old_timeout = socket.getdefaulttimeout() |
431 socket.setdefaulttimeout(timeout) | 431 socket.setdefaulttimeout(timeout) |
432 try: | 432 try: |
433 tries = 0 | 433 tries = 0 |
434 while True: | 434 while True: |
435 tries += 1 | 435 tries += 1 |
436 args = dict(kwargs) | 436 args = dict(kwargs) |
437 url = "%s%s" % (self.host, request_path) | 437 url = "%s%s" % (self.host, request_path) |
438 if args: | 438 if args: |
439 url += "?" + urllib.urlencode(args) | 439 url += "?" + urllib.urlencode(args) |
440 req = self._CreateRequest(url=url, data=payload) | 440 req = self._CreateRequest(url=url, data=payload) |
441 req.add_header("Content-Type", content_type) | 441 req.add_header("Content-Type", content_type) |
442 if extra_headers: | 442 if extra_headers: |
443 for header, value in extra_headers.items(): | 443 for header, value in extra_headers.items(): |
444 req.add_header(header, value) | 444 req.add_header(header, value) |
445 try: | 445 try: |
446 f = self.opener.open(req) | 446 f = self.opener.open(req) |
447 response = f.read() | 447 response = f.read() |
448 f.close() | 448 f.close() |
449 return response | 449 return response |
450 except urllib2.HTTPError, e: | 450 except urllib2.HTTPError, e: |
451 if tries > 3: | 451 if tries > 3: |
452 raise | 452 raise |
453 elif e.code == 401 or e.code == 302: | 453 elif e.code == 401 or e.code == 302: |
| 454 if not self.auth_function: |
| 455 raise |
454 self._Authenticate() | 456 self._Authenticate() |
455 elif e.code == 301: | 457 elif e.code == 301: |
456 # Handle permanent redirect manually. | 458 # Handle permanent redirect manually. |
457 url = e.info()["location"] | 459 url = e.info()["location"] |
458 url_loc = urlparse.urlparse(url) | 460 url_loc = urlparse.urlparse(url) |
459 self.host = '%s://%s' % (url_loc[0], url_loc[1]) | 461 self.host = '%s://%s' % (url_loc[0], url_loc[1]) |
460 elif e.code >= 500: | 462 elif e.code >= 500: |
461 ErrorExit(e.read()) | 463 ErrorExit(e.read()) |
462 else: | 464 else: |
463 raise | 465 raise |
(...skipping 2167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2631 os.environ['LC_ALL'] = 'C' | 2633 os.environ['LC_ALL'] = 'C' |
2632 RealMain(sys.argv) | 2634 RealMain(sys.argv) |
2633 except KeyboardInterrupt: | 2635 except KeyboardInterrupt: |
2634 print | 2636 print |
2635 StatusUpdate("Interrupted.") | 2637 StatusUpdate("Interrupted.") |
2636 sys.exit(1) | 2638 sys.exit(1) |
2637 | 2639 |
2638 | 2640 |
2639 if __name__ == "__main__": | 2641 if __name__ == "__main__": |
2640 main() | 2642 main() |
OLD | NEW |