本文整理匯總了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()