| /trunk/lib/utils/urlget.py r62 | /trunk/lib/utils/urlget.py r81 | ||
| 1 | # Copyright (c) 2001-2008 Twisted Matrix Laboratories. | ||
|---|---|---|---|
| 2 | # Copyright (c) 2009 Yoshizumi Endo. | ||
| 3 | # | ||
| 4 | # This urlget is a modified version of twisted.web.client module. | ||
| 5 | # | ||
| 6 | # Permission is hereby granted, free of charge, to any person obtaining | ||
| 7 | # a copy of this software and associated documentation files (the | ||
| 8 | # "Software"), to deal in the Software without restriction, including | ||
| 9 | # without limitation the rights to use, copy, modify, merge, publish, | ||
| 10 | # distribute, sublicense, and/or sell copies of the Software, and to | ||
| 11 | # permit persons to whom the Software is furnished to do so, subject to | ||
| 12 | # the following conditions: | ||
| 13 | # | ||
| 14 | # The above copyright notice and this permission notice shall be | ||
| 15 | # included in all copies or substantial portions of the Software. | ||
| 16 | # | ||
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
| 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
| 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
| 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 24 | |||
| 25 | """ | ||
| 26 | HTTP Client with proxy support. | ||
| 27 | """ | ||
| 28 | |||
| 1 | import os | 29 | import os |
| 2 | 30 | ||
| 3 | from twisted.web import client | 31 | from twisted.web import client |
| 4 | from twisted.internet import reactor | 32 | from twisted.internet import reactor |
| 5 | 33 | ||
| 6 | class UrlGetWithProxy(object): | 34 | class UrlGetWithProxy(object): |
| 7 | 35 | ||
| 8 | def __init__(self): | 36 | def __init__(self): |
| 9 | proxy = os.environ.get('http_proxy') or "" | 37 | proxy = os.environ.get('http_proxy') or "" |
| 10 | scheme, host, port, path = client._parse(proxy) | 38 | self.proxy_host, self.proxy_port = client._parse(proxy)[1:3] |
| 11 | 39 | self.use_proxy = True if self.proxy_host and self.proxy_port else False | |
| 12 | if host != "" and port != "": | ||
| 13 | self.proxy_host = host | ||
| 14 | self.proxy_port = port | ||
| 15 | self.proxyOn = True | ||
| 16 | else: | ||
| 17 | self.proxyOn = False | ||
| 18 | 40 | ||
| 19 | def getPage(self, url, contextFactory=None, *args, **kwargs): | 41 | def getPage(self, url, contextFactory=None, *args, **kwargs): |
| 20 | factory = client.HTTPClientFactory(url, *args, **kwargs) | 42 | factory = client.HTTPClientFactory(url, *args, **kwargs) |
| 21 | d = self._urlget(factory, url, file) | 43 | d = self._urlget(factory, url, file) |
| 22 | return d | 44 | return d |
| 23 | 45 | ||
| 24 | def downloadPage(self, url, file, contextFactory=None, *args, **kwargs): | 46 | def downloadPage(self, url, file, contextFactory=None, *args, **kwargs): |
| 25 | factory = client.HTTPDownloader(url, file, *args, **kwargs) | 47 | factory = client.HTTPDownloader(url, file, *args, **kwargs) |
| 26 | d = self._urlget(factory, url, file) | 48 | d = self._urlget(factory, url, contextFactory, *args, **kwargs) |
| 27 | return d | 49 | return d |
| 28 | 50 | ||
| 29 | def _urlget(self, factory, url, contextFactory=None, *args, **kwargs): | 51 | def _urlget(self, factory, url, contextFactory=None, *args, **kwargs): |
| 30 | scheme, host, port, path = client._parse(url) | 52 | scheme, host, port, path = client._parse(url) |
| 31 | if self.proxyOn is True: | 53 | if self.use_proxy: |
| 32 | host, port = self.proxy_host, self.proxy_port | 54 | host, port = self.proxy_host, self.proxy_port |
| 33 | factory.path = url | 55 | factory.path = url |
| 34 | if scheme == 'https': | 56 | if scheme == 'https': |
| 35 | from twisted.internet import ssl | 57 | from twisted.internet import ssl |
| 36 | if contextFactory is None: | 58 | if contextFactory is None: |
| 37 | contextFactory = ssl.ClientContextFactory() | 59 | contextFactory = ssl.ClientContextFactory() |
| 38 | reactor.connectSSL(host, port, factory, contextFactory) | 60 | reactor.connectSSL(host, port, factory, contextFactory) |
| 39 | else: | 61 | else: |
| 40 | reactor.connectTCP(host, port, factory) | 62 | reactor.connectTCP(host, port, factory) |
| 41 | return factory.deferred | 63 | return factory.deferred |
| 42 | |||