Python urllib2.HTTPRedirectHandler() Examples

The following are 30 code examples of urllib2.HTTPRedirectHandler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib2 , or try the search function .
Example #1
Source File: test_urllib2.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_invalid_redirect(self):
        from_url = "http://example.com/a.html"
        valid_schemes = ['http', 'https', 'ftp']
        invalid_schemes = ['file', 'imap', 'ldap']
        schemeless_url = "example.com/b.html"
        h = urllib2.HTTPRedirectHandler()
        o = h.parent = MockOpener()
        req = Request(from_url)
        req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT

        for scheme in invalid_schemes:
            invalid_url = scheme + '://' + schemeless_url
            self.assertRaises(urllib2.HTTPError, h.http_error_302,
                              req, MockFile(), 302, "Security Loophole",
                              MockHeaders({"location": invalid_url}))

        for scheme in valid_schemes:
            valid_url = scheme + '://' + schemeless_url
            h.http_error_302(req, MockFile(), 302, "That's fine",
                MockHeaders({"location": valid_url}))
            self.assertEqual(o.req.get_full_url(), valid_url) 
Example #2
Source File: sosac.py    From plugin.video.sosac.ph with GNU General Public License v2.0 6 votes vote down vote up
def probe_html5(self, result):

        class NoRedirectHandler(urllib2.HTTPRedirectHandler):

            def http_error_302(self, req, fp, code, msg, headers):
                infourl = urllib.addinfourl(fp, headers, req.get_full_url())
                infourl.status = code
                infourl.code = code
                return infourl
            http_error_300 = http_error_302
            http_error_301 = http_error_302
            http_error_303 = http_error_302
            http_error_307 = http_error_302

        opener = urllib2.build_opener(NoRedirectHandler())
        urllib2.install_opener(opener)

        r = urllib2.urlopen(urllib2.Request(result['url'], headers=result['headers']))
        if r.code == 200:
            result['url'] = r.read()
        return result 
Example #3
Source File: test_urllib2.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_invalid_redirect(self):
        from_url = "http://example.com/a.html"
        valid_schemes = ['http', 'https', 'ftp']
        invalid_schemes = ['file', 'imap', 'ldap']
        schemeless_url = "example.com/b.html"
        h = urllib2.HTTPRedirectHandler()
        o = h.parent = MockOpener()
        req = Request(from_url)
        req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT

        for scheme in invalid_schemes:
            invalid_url = scheme + '://' + schemeless_url
            self.assertRaises(urllib2.HTTPError, h.http_error_302,
                              req, MockFile(), 302, "Security Loophole",
                              MockHeaders({"location": invalid_url}))

        for scheme in valid_schemes:
            valid_url = scheme + '://' + schemeless_url
            h.http_error_302(req, MockFile(), 302, "That's fine",
                MockHeaders({"location": valid_url}))
            self.assertEqual(o.req.get_full_url(), valid_url) 
Example #4
Source File: test_urllib2.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_invalid_redirect(self):
        from_url = "http://example.com/a.html"
        valid_schemes = ['http', 'https', 'ftp']
        invalid_schemes = ['file', 'imap', 'ldap']
        schemeless_url = "example.com/b.html"
        h = urllib2.HTTPRedirectHandler()
        o = h.parent = MockOpener()
        req = Request(from_url)
        req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT

        for scheme in invalid_schemes:
            invalid_url = scheme + '://' + schemeless_url
            self.assertRaises(urllib2.HTTPError, h.http_error_302,
                              req, MockFile(), 302, "Security Loophole",
                              MockHeaders({"location": invalid_url}))

        for scheme in valid_schemes:
            valid_url = scheme + '://' + schemeless_url
            h.http_error_302(req, MockFile(), 302, "That's fine",
                MockHeaders({"location": valid_url}))
            self.assertEqual(o.req.get_full_url(), valid_url) 
Example #5
Source File: test_urllib2.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_invalid_redirect(self):
        from_url = "http://example.com/a.html"
        valid_schemes = ['http', 'https', 'ftp']
        invalid_schemes = ['file', 'imap', 'ldap']
        schemeless_url = "example.com/b.html"
        h = urllib2.HTTPRedirectHandler()
        o = h.parent = MockOpener()
        req = Request(from_url)
        req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT

        for scheme in invalid_schemes:
            invalid_url = scheme + '://' + schemeless_url
            self.assertRaises(urllib2.HTTPError, h.http_error_302,
                              req, MockFile(), 302, "Security Loophole",
                              MockHeaders({"location": invalid_url}))

        for scheme in valid_schemes:
            valid_url = scheme + '://' + schemeless_url
            h.http_error_302(req, MockFile(), 302, "That's fine",
                MockHeaders({"location": valid_url}))
            self.assertEqual(o.req.get_full_url(), valid_url) 
Example #6
Source File: test_urllib2.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_invalid_redirect(self):
        from_url = "http://example.com/a.html"
        valid_schemes = ['http', 'https', 'ftp']
        invalid_schemes = ['file', 'imap', 'ldap']
        schemeless_url = "example.com/b.html"
        h = urllib2.HTTPRedirectHandler()
        o = h.parent = MockOpener()
        req = Request(from_url)
        req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT

        for scheme in invalid_schemes:
            invalid_url = scheme + '://' + schemeless_url
            self.assertRaises(urllib2.HTTPError, h.http_error_302,
                              req, MockFile(), 302, "Security Loophole",
                              MockHeaders({"location": invalid_url}))

        for scheme in valid_schemes:
            valid_url = scheme + '://' + schemeless_url
            h.http_error_302(req, MockFile(), 302, "That's fine",
                MockHeaders({"location": valid_url}))
            self.assertEqual(o.req.get_full_url(), valid_url) 
Example #7
Source File: test_urllib2.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_invalid_redirect(self):
        from_url = "http://example.com/a.html"
        valid_schemes = ['http', 'https', 'ftp']
        invalid_schemes = ['file', 'imap', 'ldap']
        schemeless_url = "example.com/b.html"
        h = urllib2.HTTPRedirectHandler()
        o = h.parent = MockOpener()
        req = Request(from_url)
        req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT

        for scheme in invalid_schemes:
            invalid_url = scheme + '://' + schemeless_url
            self.assertRaises(urllib2.HTTPError, h.http_error_302,
                              req, MockFile(), 302, "Security Loophole",
                              MockHeaders({"location": invalid_url}))

        for scheme in valid_schemes:
            valid_url = scheme + '://' + schemeless_url
            h.http_error_302(req, MockFile(), 302, "That's fine",
                MockHeaders({"location": valid_url}))
            self.assertEqual(o.req.get_full_url(), valid_url) 
Example #8
Source File: quizduelltvapi.py    From quizduellapi with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, user_id='0', tv_auth_token='0'):
        '''
        Creates the TV API interface. The user identified by the supplied user
        id must have a TV profile created by a call to
        QuizduellApi.create_tv_user() and a personal TV auth token.
        
        @param user_id: Quizduell user id
        @type user_id: str
        
        @param tv_auth_token: TV auth token returned by
                              QuizduellApi.create_tv_user()
        @type tv_auth_token: str
        ''' 
        self._user_id = user_id
        self._tv_auth_token = tv_auth_token
        self._opener = urllib2.build_opener(
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
            urllib2.HTTPSHandler(debuglevel=0)
        ) 
Example #9
Source File: py2.py    From php-load-test with Do What The F*ck You Want To Public License 5 votes vote down vote up
def get_stock_html(URL):
        opener = urllib2.build_opener(
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
        )
        opener.addheaders = [
            ('User-agent',
             'Mozilla/4.0 (compatible;MSIE 7.0;'
             'Windows NT 5.1; .NET CLR 2.0.50727;'
             '.NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)')
        ]
        url = "http://proxy.com.ru/%s" % URL
        response = opener.open(url)
        return ''.join(response.readlines()) 
Example #10
Source File: feedparser.py    From pyrobotlab with Apache License 2.0 5 votes vote down vote up
def http_error_302(self, req, fp, code, msg, headers):
        if headers.dict.has_key('location'):
            infourl = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
        else:
            infourl = urllib.addinfourl(fp, headers, req.get_full_url())
        if not hasattr(infourl, 'status'):
            infourl.status = code
        return infourl 
Example #11
Source File: feedparser.py    From pyrobotlab with Apache License 2.0 5 votes vote down vote up
def http_error_301(self, req, fp, code, msg, headers):
        if headers.dict.has_key('location'):
            infourl = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
        else:
            infourl = urllib.addinfourl(fp, headers, req.get_full_url())
        if not hasattr(infourl, 'status'):
            infourl.status = code
        return infourl 
Example #12
Source File: UcsHandle_Edit.py    From UcsPythonSDK with Apache License 2.0 5 votes vote down vote up
def http_error_301(self, req, fp, code, msg, headers):
		# result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
		respStatus = [code, headers.dict["location"]]
		return respStatus 
Example #13
Source File: UcsHandle_Edit.py    From UcsPythonSDK with Apache License 2.0 5 votes vote down vote up
def http_error_302(self, req, fp, code, msg, headers):
		# result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
		respStatus = [code, headers.dict["location"]]
		# print respStatus
		return respStatus 
Example #14
Source File: UcsHandle.py    From UcsPythonSDK with Apache License 2.0 5 votes vote down vote up
def http_error_301(self, req, fp, code, msg, headers):
		# result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
		respStatus = [code, headers.dict["location"]]
		return respStatus 
Example #15
Source File: UcsHandle.py    From UcsPythonSDK with Apache License 2.0 5 votes vote down vote up
def http_error_302(self, req, fp, code, msg, headers):
		# result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
		respStatus = [code, headers.dict["location"]]
		# print respStatus
		return respStatus 
Example #16
Source File: quizduellapi.py    From quizduellapi with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, cookie_jar=None):
        '''
        Creates the API interface. Expects either an authentication cookie within
        the user supplied cookie jar or a subsequent call to
        QuizduellApi.login_user() or QuizduellApi.create_user().
        
        @param cookie_jar: Stores authentication tokens with each request made
        @type cookie_jar: cookielib.CookieJar or None
        '''
        self._opener = urllib2.build_opener(
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
            urllib2.HTTPSHandler(debuglevel=0),
            urllib2.HTTPCookieProcessor(cookie_jar)
        ) 
Example #17
Source File: feedparser.py    From xbmc-addons-chinese with GNU General Public License v2.0 5 votes vote down vote up
def http_error_301(self, req, fp, code, msg, hdrs):
        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp,
                                                            code, msg, hdrs)
        result.status = code
        result.newurl = result.geturl()
        return result
    # The default implementations in urllib2.HTTPRedirectHandler
    # are identical, so hardcoding a http_error_301 call above
    # won't affect anything 
Example #18
Source File: py3.py    From php-load-test with Do What The F*ck You Want To Public License 5 votes vote down vote up
def get_stock_html(URL):
        opener = urllib2.build_opener(
                urllib2.HTTPRedirectHandler(),
                urllib2.HTTPHandler(debuglevel=0),
                )
        opener.addheaders = [
                ('User-agent',
                 'Mozilla/4.0 (compatible;MSIE 7.0;'
                 'Windows NT 5.1; .NET CLR 2.0.50727;'
                 '.NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)')
                 ]
        url = "http://proxy.com.ru/%s" % URL
        response = opener.open(url)
        return str(b''.join(response.readlines())) 
Example #19
Source File: test_urllib2.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_redirect_fragment(self):
        redirected_url = 'http://www.example.com/index.html#OK\r\n\r\n'
        hh = MockHTTPHandler(302, 'Location: ' + redirected_url)
        hdeh = urllib2.HTTPDefaultErrorHandler()
        hrh = urllib2.HTTPRedirectHandler()
        o = build_test_opener(hh, hdeh, hrh)
        fp = o.open('http://www.example.com')
        self.assertEqual(fp.geturl(), redirected_url.strip()) 
Example #20
Source File: test_urllib2.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cookie_redirect(self):
        # cookies shouldn't leak into redirected requests
        from cookielib import CookieJar

        from test.test_cookielib import interact_netscape

        cj = CookieJar()
        interact_netscape(cj, "http://www.example.com/", "spam=eggs")
        hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
        hdeh = urllib2.HTTPDefaultErrorHandler()
        hrh = urllib2.HTTPRedirectHandler()
        cp = urllib2.HTTPCookieProcessor(cj)
        o = build_test_opener(hh, hdeh, hrh, cp)
        o.open("http://www.example.com/")
        self.assertTrue(not hh.req.has_header("Cookie")) 
Example #21
Source File: test_urllib2.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_redirect_fragment(self):
        redirected_url = 'http://www.example.com/index.html#OK\r\n\r\n'
        hh = MockHTTPHandler(302, 'Location: ' + redirected_url)
        hdeh = urllib2.HTTPDefaultErrorHandler()
        hrh = urllib2.HTTPRedirectHandler()
        o = build_test_opener(hh, hdeh, hrh)
        fp = o.open('http://www.example.com')
        self.assertEqual(fp.geturl(), redirected_url.strip()) 
Example #22
Source File: test_urllib2.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_cookie_redirect(self):
        # cookies shouldn't leak into redirected requests
        from cookielib import CookieJar

        from test.test_cookielib import interact_netscape

        cj = CookieJar()
        interact_netscape(cj, "http://www.example.com/", "spam=eggs")
        hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
        hdeh = urllib2.HTTPDefaultErrorHandler()
        hrh = urllib2.HTTPRedirectHandler()
        cp = urllib2.HTTPCookieProcessor(cj)
        o = build_test_opener(hh, hdeh, hrh, cp)
        o.open("http://www.example.com/")
        self.assert_(not hh.req.has_header("Cookie")) 
Example #23
Source File: test_urllib2.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_cookie_redirect(self):
        # cookies shouldn't leak into redirected requests
        from cookielib import CookieJar

        from test.test_cookielib import interact_netscape

        cj = CookieJar()
        interact_netscape(cj, "http://www.example.com/", "spam=eggs")
        hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
        hdeh = urllib2.HTTPDefaultErrorHandler()
        hrh = urllib2.HTTPRedirectHandler()
        cp = urllib2.HTTPCookieProcessor(cj)
        o = build_test_opener(hh, hdeh, hrh, cp)
        o.open("http://www.example.com/")
        self.assertTrue(not hh.req.has_header("Cookie")) 
Example #24
Source File: test_urllib2.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_redirect_fragment(self):
        redirected_url = 'http://www.example.com/index.html#OK\r\n\r\n'
        hh = MockHTTPHandler(302, 'Location: ' + redirected_url)
        hdeh = urllib2.HTTPDefaultErrorHandler()
        hrh = urllib2.HTTPRedirectHandler()
        o = build_test_opener(hh, hdeh, hrh)
        fp = o.open('http://www.example.com')
        self.assertEqual(fp.geturl(), redirected_url.strip()) 
Example #25
Source File: feedparser.py    From termite-visualizations with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def http_error_301(self, req, fp, code, msg, hdrs):
        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp,
                                                            code, msg, hdrs)
        result.status = code
        result.newurl = result.geturl()
        return result
    # The default implementations in urllib2.HTTPRedirectHandler
    # are identical, so hardcoding a http_error_301 call above
    # won't affect anything 
Example #26
Source File: feedparser.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def http_error_301(self, req, fp, code, msg, hdrs):
        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp,
                                                            code, msg, hdrs)
        result.status = code
        result.newurl = result.geturl()
        return result
    # The default implementations in urllib2.HTTPRedirectHandler
    # are identical, so hardcoding a http_error_301 call above
    # won't affect anything 
Example #27
Source File: test_urllib2.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_cookie_redirect(self):
        # cookies shouldn't leak into redirected requests
        from cookielib import CookieJar

        from test.test_cookielib import interact_netscape

        cj = CookieJar()
        interact_netscape(cj, "http://www.example.com/", "spam=eggs")
        hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
        hdeh = urllib2.HTTPDefaultErrorHandler()
        hrh = urllib2.HTTPRedirectHandler()
        cp = urllib2.HTTPCookieProcessor(cj)
        o = build_test_opener(hh, hdeh, hrh, cp)
        o.open("http://www.example.com/")
        self.assertTrue(not hh.req.has_header("Cookie")) 
Example #28
Source File: test_urllib2.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_redirect_fragment(self):
        redirected_url = 'http://www.example.com/index.html#OK\r\n\r\n'
        hh = MockHTTPHandler(302, 'Location: ' + redirected_url)
        hdeh = urllib2.HTTPDefaultErrorHandler()
        hrh = urllib2.HTTPRedirectHandler()
        o = build_test_opener(hh, hdeh, hrh)
        fp = o.open('http://www.example.com')
        self.assertEqual(fp.geturl(), redirected_url.strip()) 
Example #29
Source File: tvtime.py    From script.tvtime with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, token, show_id):
        self.token = token
        self.show_id = show_id
        self.action = 'follow'
        request_data = urllib.urlencode({
            'access_token' : self.token,
            'show_id' : self.show_id
            })
        
        self.cj = cookielib.CookieJar()
        self.opener = urllib2.build_opener(
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
            urllib2.HTTPSHandler(debuglevel=0),
            urllib2.HTTPCookieProcessor(self.cj)
        )
        self.opener.addheaders = [
            ('User-agent', 'Lynx/2.8.1pre.9 libwww-FM/2.14')
        ]
                           
        self.opener.get_method = lambda: 'POST'
             
        request_url = "%s%s" % (request_uri, self.action)
        try:
            response = self.opener.open(request_url, request_data)
            data = json.loads(''.join(response.readlines()))
        except:
            data = None
        
        if (data is None) or (data['result'] == "KO"):
           self.is_follow = False
        else:
           self.is_follow = True 
Example #30
Source File: hsecscan.py    From hsecscan with GNU General Public License v2.0 5 votes vote down vote up
def redirect_request(self, req, fp, code, msg, headers, newurl):
        newreq = urllib2.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, headers, newurl)
        print '>> REDIRECT INFO <<'
        print_response(req.get_full_url(), code, headers)
        print '>> REDIRECT HEADERS DETAILS <<'
        for header in headers.items():
            check_header(header)
        print '>> REDIRECT MISSING HEADERS <<'
        missing_headers(headers.items(), urlparse(newurl).scheme)
        return newreq