本文整理汇总了Python中urllib2.HTTPPasswordMgrWithDefaultRealm类的典型用法代码示例。如果您正苦于以下问题:Python HTTPPasswordMgrWithDefaultRealm类的具体用法?Python HTTPPasswordMgrWithDefaultRealm怎么用?Python HTTPPasswordMgrWithDefaultRealm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HTTPPasswordMgrWithDefaultRealm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_message
def send_message(self, message):
headers = {'Content-Type' : 'application/soap+xml;charset=UTF-8',
'Content-Length' : len(message),
'User-Agent' : 'Python WinRM client'}
password_manager = HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, self.endpoint, self.username, self.password)
auth_manager = HTTPBasicAuthHandler(password_manager)
opener = build_opener(auth_manager)
install_opener(opener)
request = Request(self.endpoint, data=message, headers=headers)
try:
response = urlopen(request, timeout=self.timeout)
# Version 1.1 of WinRM adds the namespaces in the document instead of the envelope so we have to
# add them ourselves here. This should have no affect version 2.
response_text = response.read()
return response_text
#doc = ElementTree.fromstring(response.read())
#Ruby
#doc = Nokogiri::XML(resp.http_body.content)
#doc.collect_namespaces.each_pair do |k,v|
# doc.root.add_namespace((k.split(/:/).last),v) unless doc.namespaces.has_key?(k)
#end
#return doc
#return doc
except HTTPError as ex:
error_message = 'Bad HTTP response returned from server. Code {0}'.format(ex.code)
if ex.msg:
error_message += ', {0}'.format(ex.msg)
raise WinRMTransportError(error_message)
except URLError as ex:
raise WinRMTransportError(ex.reason)
示例2: get
def get(self, date):
"""
Get data
"""
from lxml import html
from urllib2 import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, build_opener, urlopen
if not self.verify:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
url = self.get_url(date)
if self.auth:
password_mgr = HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, self.auth["username"], self.auth["password"])
handler = HTTPBasicAuthHandler(password_mgr)
opener = build_opener(handler)
src = opener.open(url).read()
else:
src = urlopen(url).read()
return html.fromstring(src)
示例3: _setup_opener
def _setup_opener(self):
password_manager = HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(
None, self.endpoint, self.username, self.password)
auth_manager = HTTPBasicAuthHandler(password_manager)
opener = build_opener(auth_manager)
install_opener(opener)
示例4: __init__
def __init__(self,link,username,password):
self.url = link
ps_mgr = HTTPPasswordMgrWithDefaultRealm()
ps_mgr.add_password(None, self.url, username, password)
handler = HTTPBasicAuthHandler(ps_mgr)
opener = build_opener(handler)
install_opener(opener)
示例5: get
def get(self,
url,
username=None,
password=None,
**kwargs):
""" Makes a get request to the URL specified."""
if kwargs:
if url.find('?') >= 0:
url = url + '&' + urlencode(kwargs)
else:
url = url + '?' + urlencode(kwargs)
request = RESTRequest(url, method='GET')
# add a user-agent
request.add_header('User-Agent', self.user_agent)
# create a password manager
passwordManager = HTTPPasswordMgrWithDefaultRealm()
passwordManager.add_password(None, url, username, password)
opener = build_opener(SmartRedirectHandler(),
DefaultErrorHandler(),
ContextualBasicAuthHandler(passwordManager))
return opener.open(request)
示例6: _URLOpenBehindProxyAuthentication
def _URLOpenBehindProxyAuthentication(self, request, post_dict):
'''
Open the given request providing support for proxy authentication.
:param Request request:
The URL request.
:param str post_dict:
String with a sequence of two-element tuples or dict encoded for POST requests.
See urllib.urlencode.
:rtype: file
:returns:
The file containing the url request contents.
'''
user, password = self._on_proxy_authentication_request(request)
password_manager = HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, self._on_proxy_address_request(), user, password)
proxy_authentication_handler = ProxyBasicAuthHandler(password_manager)
opener = urllib2.build_opener(proxy_authentication_handler)
urllib2.install_opener(opener)
result = urllib2.urlopen(request, post_dict)
return result
示例7: delete
def delete(self, url, username=None, password=None, **kwargs):
""" Makes a delete request to the URL specified. """
if kwargs:
if url.find('?') >= 0:
url = url + '&' + urlencode(kwargs)
else:
url = url + '?' + urlencode(kwargs)
request = RESTRequest(url, method='DELETE')
# add a user-agent
request.add_header('User-Agent', self.user_agent)
# create a password manager
passwordManager = HTTPPasswordMgrWithDefaultRealm()
passwordManager.add_password(None, url, username, password)
opener = build_opener(SmartRedirectHandler(),
DefaultErrorHandler(),
ContextualBasicAuthHandler(passwordManager))
#try:
# opener.open(request)
#except urllib2.HTTPError, e:
# if e.code is not 204:
# raise e
#return None
return opener.open(request)
示例8: openURL
def openURL(url_base, data, method='Get', cookies=None, username=None, password=None):
''' function to open urls - wrapper around urllib2.urlopen but with additional checks for OGC service exceptions and url formatting, also handles cookies and simple user password authentication'''
url_base.strip()
lastchar = url_base[-1]
if lastchar not in ['?', '&']:
if url_base.find('?') == -1:
url_base = url_base + '?'
else:
url_base = url_base + '&'
if username and password:
# Provide login information in order to use the WMS server
# Create an OpenerDirector with support for Basic HTTP
# Authentication...
passman = HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url_base, username, password)
auth_handler = HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(auth_handler)
openit = opener.open
else:
openit = urlopen
try:
if method == 'Post':
req = Request(url_base, data)
else:
req=Request(url_base + data)
if cookies is not None:
req.add_header('Cookie', cookies)
u = openit(req)
except HTTPError, e: #Some servers may set the http header to 400 if returning an OGC service exception or 401 if unauthorised.
if e.code in [400, 401]:
raise ServiceException, e.read()
else:
raise e
示例9: check_password
def check_password(self, username, password):
self.log.debug("Trac.ini authentication_url = '%s'" % self.auth_url)
# Nothing to do, if URL is absolute.
if self.auth_url.startswith('http://') or \
self.auth_url.startswith('https://'):
authUrl = self.auth_url
# Handle server-relative URLs.
elif self.auth_url.startswith('/'):
# Prepend the Trac server component.
pr = urlparse(self.env.abs_href())
href = Href(pr[0] + '://' + pr[1])
authUrl = href(self.auth_url)
elif '/' in self.auth_url:
# URLs with path like 'common/authFile' or 'site/authFile'.
authUrl = self.env.abs_href.chrome(self.auth_url)
else:
# Bare file name option value like 'authFile'.
authUrl = self.env.abs_href.chrome('common', self.auth_url)
self.log.debug("Final auth_url = '%s'" % authUrl)
acctmgr = HTTPPasswordMgrWithDefaultRealm()
acctmgr.add_password(None, authUrl, username, password)
try:
build_opener(HTTPBasicAuthHandler(acctmgr),
HTTPDigestAuthHandler(acctmgr)).open(authUrl)
except IOError,e:
if hasattr(e, 'code') and e.code == 404:
self.log.debug("""HttpAuthStore page not found; we are
authenticated nonetheless""")
return True
if hasattr(e, 'code') and e.code == 401:
self.log.debug("HttpAuthStore authentication failed")
return None
示例10: openURL
def openURL(url_base, data, method='Get', cookies=None, username=None, password=None, timeout=30):
''' function to open urls - wrapper around urllib2.urlopen but with additional checks for OGC service exceptions and url formatting, also handles cookies and simple user password authentication'''
url_base.strip()
lastchar = url_base[-1]
if lastchar not in ['?', '&']:
if url_base.find('?') == -1:
url_base = url_base + '?'
else:
url_base = url_base + '&'
if username and password:
# Provide login information in order to use the WMS server
# Create an OpenerDirector with support for Basic HTTP
# Authentication...
passman = HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url_base, username, password)
auth_handler = HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(auth_handler)
openit = opener.open
else:
# NOTE: optionally set debuglevel>0 to debug HTTP connection
#opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=0))
#openit = opener.open
openit = urlopen
try:
if method == 'Post':
req = Request(url_base, data)
# set appropriate header if posting XML
try:
xml = etree.fromstring(data)
req.add_header('Content-Type', "text/xml")
except:
pass
else:
req=Request(url_base + data)
if cookies is not None:
req.add_header('Cookie', cookies)
u = openit(req, timeout=timeout)
except HTTPError as e: #Some servers may set the http header to 400 if returning an OGC service exception or 401 if unauthorised.
if e.code in [400, 401]:
raise ServiceException(e.read())
else:
raise e
# check for service exceptions without the http header set
if 'Content-Type' in u.info() and u.info()['Content-Type'] in ['text/xml', 'application/xml']:
#just in case 400 headers were not set, going to have to read the xml to see if it's an exception report.
#wrap the url stram in a extended StringIO object so it's re-readable
u=RereadableURL(u)
se_xml= u.read()
se_tree = etree.fromstring(se_xml)
serviceException=se_tree.find('{http://www.opengis.net/ows}Exception')
if serviceException is None:
serviceException=se_tree.find('ServiceException')
if serviceException is not None:
raise ServiceException(str(serviceException.text).strip())
u.seek(0) #return cursor to start of u
return u
示例11: __init__
def __init__(self, username, password, ip, type=None):
self.username = username
self.password = password
self.ip = ip
password_mgr = HTTPPasswordMgrWithDefaultRealm()
handler = HTTPBasicAuthHandler(password_mgr)
password_mgr.add_password(None, self.ip, self.username, self.password)
self.opener = build_opener(handler)
pass
示例12: check_password
def check_password(self, user, password):
mgr = HTTPPasswordMgrWithDefaultRealm()
mgr.add_password(None, self.auth_url, user, password)
try:
build_opener(HTTPBasicAuthHandler(mgr),
HTTPDigestAuthHandler(mgr)).open(self.auth_url)
except IOError:
return False
else:
return True
示例13: _setup_opener
def _setup_opener(self, **kwargs):
password_manager = HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(
None, self.endpoint, self.username, self.password)
auth_manager = ForcedBasicAuthHandler(password_manager)
handlers = [auth_manager]
root_handler = kwargs.get('root_handler')
if root_handler:
handlers.insert(0, root_handler)
self.opener = build_opener(*handlers)
示例14: post
def post(self,
url,
payload,
contentType,
username=None,
password=None,
**kwargs):
"""
Makes a POST request to the URL specified and posts the payload
that gets passed in. The content type header gets set to the
specified content type.
"""
headers = None
if kwargs:
if 'headers' in kwargs:
headers = kwargs['headers']
del(kwargs['headers'])
self.logger.debug('Headers passed in:%s' % headers)
if url.find('?') >= 0:
url = url + '&' + urlencode(kwargs)
else:
url = url + '?' + urlencode(kwargs)
self.logger.debug('About to do a POST on:' + url)
request = RESTRequest(url, payload, method='POST')
# set the content type header
request.add_header('Content-Type', contentType)
# add a user-agent
request.add_header('User-Agent', self.user_agent)
if headers:
for k, v in headers.items():
self.logger.debug('Adding header:%s:%s' % (k, v))
request.add_header(k, v)
# create a password manager
passwordManager = HTTPPasswordMgrWithDefaultRealm()
passwordManager.add_password(None, url, username, password)
opener = build_opener(SmartRedirectHandler(),
DefaultErrorHandler(),
ContextualBasicAuthHandler(passwordManager))
try:
return opener.open(request)
except HTTPError, e:
if e.code is not 201:
return e
else:
return e.read()
示例15: getOpener
def getOpener(self):
"""
Return a custom urllib2 opener for logged request to supervisor.
"""
from urllib2 import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener, install_opener
passman = HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, self.index, self.login, self.password)
authhandler = HTTPBasicAuthHandler(passman)
opener = build_opener(authhandler)
install_opener(opener)
return opener