My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
# Copyright (c) 2009-2011 Yoshizumi Endo.
#
# This urlget is a modified version of twisted.web.client module.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
HTTP Client with proxy support.
"""

import os

from twisted.web import client
from twisted.internet import reactor


class UrlGetWithProxy(object):

def __init__(self, proxy=None):
if proxy is None:
proxy = os.environ.get('http_proxy') or ""
self.proxy_host, self.proxy_port = client._parse(proxy)[1:3]
self.use_proxy = bool(self.proxy_host and self.proxy_port)

def getPage(self, url, contextFactory=None, *args, **kwargs):
factory = client.HTTPClientFactory(url, *args, **kwargs)
d = self._urlget(factory, url, contextFactory)
return d

def downloadPage(self, url, file, contextFactory=None, *args, **kwargs):
factory = client.HTTPDownloader(url, file, *args, **kwargs)
d = self._urlget(factory, url, contextFactory)
return d

def _urlget(self, factory, url, contextFactory=None):
scheme, host, port, path = client._parse(url)
if self.use_proxy and scheme != 'https': # HTTPS proxy isn't supported.
host, port = self.proxy_host, self.proxy_port
factory.path = url
if scheme == 'https':
from twisted.internet import ssl
if contextFactory is None:
contextFactory = ssl.ClientContextFactory()
reactor.connectSSL(host, port, factory, contextFactory)
else:
reactor.connectTCP(host, port, factory)
return factory.deferred

Change log

09eb9764c19d by yendo0206 on Dec 7, 2011   Diff
Code refactoring.
Go to: 
Project members, sign in to write a code review

Older revisions

ddb33d6a6895 by yendo0206 on Feb 17, 2011   Diff
Miscellaneous modifications.
ae87365312e4 by yendo0206 on Feb 3, 2011   Diff
Code refactoring.
dc4e6839cbbb by yendo0206 on Feb 1, 2011   Diff
Fixed a bug.
All revisions of this file

File info

Size: 2676 bytes, 65 lines
Powered by Google Project Hosting