| /trunk/lib/urlget.py r17 | /trunk/lib/utils/urlget.py r62 | ||
| Properties | |||
| svn:mergeinfo | |||
| Contents | |||
| 1 | import os | 1 | import os |
| 2 | |||
| 2 | from twisted.web import client | 3 | from twisted.web import client |
| 3 | from twisted.internet import reactor | 4 | from twisted.internet import reactor |
| 4 | 5 | ||
| 5 | class UrlGetWithProxy(object): | 6 | class UrlGetWithProxy(object): |
| 6 | 7 | ||
| 7 | def __init__(self): | 8 | def __init__(self): |
| 8 | proxy = os.environ.get('http_proxy') or "" | 9 | proxy = os.environ.get('http_proxy') or "" |
| 9 | scheme, host, port, path = client._parse(proxy) | 10 | scheme, host, port, path = client._parse(proxy) |
| 10 | 11 | ||
| 11 | if host != "" and port != "": | 12 | if host != "" and port != "": |
| 12 | self.proxy_host = host | 13 | self.proxy_host = host |
| 13 | self.proxy_port = port | 14 | self.proxy_port = port |
| 14 | self.proxyOn = True | 15 | self.proxyOn = True |
| 15 | else: | 16 | else: |
| 16 | self.proxyOn = False | 17 | self.proxyOn = False |
| 17 | 18 | ||
| 18 | def getPage(self, url, contextFactory=None, *args, **kwargs): | 19 | def getPage(self, url, contextFactory=None, *args, **kwargs): |
| 19 | factory = client.HTTPClientFactory(url, *args, **kwargs) | 20 | factory = client.HTTPClientFactory(url, *args, **kwargs) |
| 20 | d = self._urlget(factory, url, file) | 21 | d = self._urlget(factory, url, file) |
| 21 | return d | 22 | return d |
| 22 | 23 | ||
| 23 | def downloadPage(self, url, file, contextFactory=None, *args, **kwargs): | 24 | def downloadPage(self, url, file, contextFactory=None, *args, **kwargs): |
| 24 | factory = client.HTTPDownloader(url, file, *args, **kwargs) | 25 | factory = client.HTTPDownloader(url, file, *args, **kwargs) |
| 25 | d = self._urlget(factory, url, file) | 26 | d = self._urlget(factory, url, file) |
| 26 | return d | 27 | return d |
| 27 | 28 | ||
| 28 | def _urlget(self, factory, url, contextFactory=None, *args, **kwargs): | 29 | def _urlget(self, factory, url, contextFactory=None, *args, **kwargs): |
| 29 | scheme, host, port, path = client._parse(url) | 30 | scheme, host, port, path = client._parse(url) |
| 30 | if self.proxyOn is True: | 31 | if self.proxyOn is True: |
| 31 | host, port = self.proxy_host, self.proxy_port | 32 | host, port = self.proxy_host, self.proxy_port |
| 32 | factory.path = url | 33 | factory.path = url |
| 33 | if scheme == 'https': | 34 | if scheme == 'https': |
| 34 | from twisted.internet import ssl | 35 | from twisted.internet import ssl |
| 35 | if contextFactory is None: | 36 | if contextFactory is None: |
| 36 | contextFactory = ssl.ClientContextFactory() | 37 | contextFactory = ssl.ClientContextFactory() |
| 37 | reactor.connectSSL(host, port, factory, contextFactory) | 38 | reactor.connectSSL(host, port, factory, contextFactory) |
| 38 | else: | 39 | else: |
| 39 | reactor.connectTCP(host, port, factory) | 40 | reactor.connectTCP(host, port, factory) |
| 40 | return factory.deferred | 41 | return factory.deferred |
| 41 | 42 | ||