本文整理汇总了Python中urllib2.HTTPRedirectHandler方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.HTTPRedirectHandler方法的具体用法?Python urllib2.HTTPRedirectHandler怎么用?Python urllib2.HTTPRedirectHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.HTTPRedirectHandler方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: probe_html5
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPRedirectHandler [as 别名]
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
示例2: test_invalid_redirect
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPRedirectHandler [as 别名]
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)
示例3: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPRedirectHandler [as 别名]
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)
)
示例4: redirect_request
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPRedirectHandler [as 别名]
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
示例5: test_cookie_redirect
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPRedirectHandler [as 别名]
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"))
示例6: test_redirect_fragment
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPRedirectHandler [as 别名]
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())
示例7: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPRedirectHandler [as 别名]
def __init__(self, token, episode_id, filename=''):
self.token = token
self.episode_id = episode_id
self.filename = filename
if len(self.filename) > 0:
self.action = 'episode?access_token=%s&filename=%s' % (self.token, self.filename)
else:
self.action = 'episode?access_token=%s&episode_id=%s' % (self.token, self.episode_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: 'GET'
request_url = "%s%s" % (request_uri, self.action)
try:
response = self.opener.open(request_url, None)
data = json.loads(''.join(response.readlines()))
except:
data = None
if (data is None) or (data['result'] == "KO"):
self.is_found = False
else:
self.is_found = True
self.resultdata = data['result']
self.showname = data['episode']['show']['name']
self.episodename = data['episode']['name']
self.season_number = data['episode']['season_number']
self.number = data['episode']['number']
self.id = data['episode']['id']
示例8: http_error_301
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPRedirectHandler [as 别名]
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