本文整理汇总了Python中urllib2.OpenerDirector方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.OpenerDirector方法的具体用法?Python urllib2.OpenerDirector怎么用?Python urllib2.OpenerDirector使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.OpenerDirector方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: openerHeaders
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import OpenerDirector [as 别名]
def openerHeaders(op):
headers = {}
try:
assert isinstance(op, urllib2.OpenerDirector)
_ = op.addheaders
for pair in _:
# pair_copy = [part for part in pair]
headers.update({pair[0]: pair[1]})
except:
errMsg = 'unable to fetch headers from given opener'
logger.log(CUSTOM_LOGGING.ERROR, errMsg)
return headers
示例2: _GetOpener
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import OpenerDirector [as 别名]
def _GetOpener(self):
"""Returns an OpenerDirector for making HTTP requests.
Returns:
A urllib2.OpenerDirector object.
"""
raise NotImplementedError
示例3: _GetOpener
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import OpenerDirector [as 别名]
def _GetOpener(self):
"""Returns an OpenerDirector for making HTTP requests.
Returns:
A urllib2.OpenerDirector object.
"""
raise NotImplementedError()
示例4: test_base_fs_creation
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import OpenerDirector [as 别名]
def test_base_fs_creation(self):
fs = familysearch.FamilySearch(self.agent, self.devkey)
self.assertTrue(fs.base == 'https://sandbox.familysearch.org')
print("Base is correct.")
self.assertTrue(fs.key == self.devkey)
print("Key is correct.")
self.assertTrue(isinstance(fs.opener, request.OpenerDirector))
print("HTTP opener works.")
self.assertTrue(fs.access_token == None)
print("Access token works.")
示例5: _GetOpener
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import OpenerDirector [as 别名]
def _GetOpener(self):
"""Returns an OpenerDirector that supports cookies and ignores redirects.
Returns:
A urllib2.OpenerDirector object.
"""
opener = appengine_rpc.HttpRpcServer._GetOpener(self)
opener.add_handler(ThrottleHandler(self.throttle))
return opener
示例6: testAuth
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import OpenerDirector [as 别名]
def testAuth(self):
# CNY-981
try:
contentServer = rephelp.HTTPServerController(authRequester())
baseUrl = contentServer.url()
# test with user:pass
contentURL = 'http://user:pass@%s' % httplib.urlsplit(baseUrl)[1]
name = 'foo.tar.gz'
url = contentURL + '/' + name
cached = lookaside.fetchURL(self.cfg, url, name)
f = open(cached, 'r')
self.assertEqual(f.read(), 'Hello, world!\n')
# test with no password given
contentURL = 'http://user@%s' % httplib.urlsplit(baseUrl)[1]
name = 'foo2.tar.gz'
url = contentURL + '/' + name
cached = lookaside.fetchURL(self.cfg, url, name)
f = open(cached, 'r')
self.assertEqual(f.read(), 'Hello, world 2!\n')
# test with no password at all
name = 'foo3.tar.gz'
url = baseUrl + '/' + name
cached = self.logCheck(lookaside.fetchURL, (self.cfg, url, name),
['error: error downloading http://localhost:[0-9]*//foo3.tar.gz: HTTP Error 401: Unauthorized'],
regExp=True)
self.assertEqual(cached, None)
# test ftp with user:pass
def fakeOpen(od, req, *args, **kw):
self.req = req
import StringIO
s = 'baz file contents'
r = StringIO.StringIO(s)
r.headers = {'contentLength': len(s)}
return r
import urllib2
self.mock(urllib2.OpenerDirector, 'open', fakeOpen)
url = 'ftp://user:pass@foo.com/bar/baz.tgz'
name = 'baz.tgz'
cached = lookaside.fetchURL(self.cfg, url, name)
self.assertEqual(url, self.req.get_full_url())
self.assertEqual(open(cached).read(), 'baz file contents')
finally:
contentServer.kill()