本文整理匯總了Python中requests_file.FileAdapter方法的典型用法代碼示例。如果您正苦於以下問題:Python requests_file.FileAdapter方法的具體用法?Python requests_file.FileAdapter怎麽用?Python requests_file.FileAdapter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類requests_file
的用法示例。
在下文中一共展示了requests_file.FileAdapter方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: from_url
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def from_url(cls, url, keys=None, encoding='utf-8'):
"""
Initialize a new :py:class:`.Catalog` object from a resource at the
specified URL. The resulting data is validated against a schema file
with :py:func:`~king_phisher.utilities.validate_json_schema` before
being passed to :py:meth:`~.__init__`.
:param str url: The URL to the catalog data to load.
:param keys: The keys to use for verifying remote data.
:type keys: :py:class:`~king_phisher.security_keys.SecurityKeys`
:param str encoding: The encoding of the catalog data.
:return: The new catalog instance.
:rtype: :py:class:`.Catalog`
"""
keys = keys or security_keys.SecurityKeys()
req_sess = requests.Session()
req_sess.mount('file://', requests_file.FileAdapter())
cls.logger.debug('fetching catalog from: ' + url)
resp = req_sess.get(url)
data = resp.content.decode(encoding)
data = serializers.JSON.loads(data)
utilities.validate_json_schema(data, 'king-phisher.catalog')
keys.verify_dict(data, signature_encoding='base64')
return cls(data, keys=keys)
示例2: find_first_response
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def find_first_response(urls):
""" Decode the first successfully fetched URL, from UTF-8 encoding to
Python unicode.
"""
text = ''
for url in urls:
try:
session = requests.Session()
session.mount('file://', FileAdapter())
text = session.get(url).text
except requests.exceptions.RequestException as ree:
LOG.error('Exception reading Public Suffix List url ' + url + ' - ' + str(ree) + '.')
else:
return _decode_utf8(text)
LOG.error(
'No Public Suffix List found. Consider using a mirror or constructing '
'your TLDExtract with `suffix_list_urls=None`.'
)
return unicode('')
示例3: processSwaggerURL
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def processSwaggerURL(self, url):
parsed_url = urlparse.urlparse(url)
if not parsed_url.scheme: # Assume file relative to documentation
env = self.state.document.settings.env
relfn, absfn = env.relfn2path(url)
env.note_dependency(relfn)
with open(absfn) as fd:
content = fd.read()
return json.loads(content)
else:
s = requests.Session()
s.mount('file://', FileAdapter())
r = s.get(url)
return r.json()
示例4: check_to_open
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def check_to_open(filename):
"""check if `filename` is a fetchable uri and returns True in the case is true False otherwise"""
url_name = urllify(filename)
with r.Session() as r_session:
r_session.mount('file://', requests_file.FileAdapter())
f = r_session.get(url_name, stream=True)
is_ok = True
try:
f.raise_for_status()
except Exception as e:
is_ok = False
finally:
f.close()
return is_ok
示例5: _func_fetch
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def _func_fetch(self, url, allow_file=False):
session = requests.Session()
if allow_file:
session.mount('file://', requests_file.FileAdapter())
try:
response = session.get(url)
except requests.exceptions.RequestException:
self.logger.error('template failed to load url: ' + url)
return None
return response.text
示例6: perform_download_packages_from_repository
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def perform_download_packages_from_repository(dispatcher, intent):
"""
See :class:`DownloadPackagesFromRepository`.
"""
rpm_version = make_rpm_version(intent.flocker_version)
package_type = intent.distribution.package_type()
s = requests.Session()
# Tests use a local package repository
s.mount('file://', FileAdapter())
downloaded_packages = set()
for package in intent.packages:
package_name = package_filename(
package_type=package_type,
package=package,
architecture=PACKAGE_ARCHITECTURE[package],
rpm_version=rpm_version,
)
url = intent.source_repo + '/' + package_name
local_path = intent.target_path.child(package_name).path
download = s.get(url)
download.raise_for_status()
content = download.content
with open(local_path, "wb") as local_file:
local_file.write(content)
downloaded_packages.add(package_name)
return downloaded_packages
示例7: async_get
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def async_get(event_loop):
""" AsyncSession cannot be created global since it will create
a different loop from pytest-asyncio. """
async_session = AsyncXMLSession()
async_session.mount('file:///', FileAdapter())
path = os.path.sep.join((os.path.dirname(os.path.abspath(__file__)), 'nasa.rss'))
url = 'file:///{}'.format(path)
return partial(async_session.get, url)
示例8: Playlistparser
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def Playlistparser(self):
try:
s = requests.Session()
s.mount('file://', FileAdapter())
with s.get(config.url, headers=self.headers, proxies=config.proxies, stream=False, timeout=30) as r:
if r.status_code != 304:
if r.encoding is None: r.encoding = 'utf-8'
self.headers['If-Modified-Since'] = gevent.time.strftime('%a, %d %b %Y %H:%M:%S %Z', gevent.time.gmtime(self.playlisttime))
self.playlist = PlaylistGenerator(m3uchanneltemplate=config.m3uchanneltemplate)
self.picons = picons.logomap
self.channels = {}
m = requests.auth.hashlib.md5()
logging.info('[%s]: playlist %s downloaded' % (self.__class__.__name__, config.url))
pattern = requests.utils.re.compile(r',(?P<name>.+)[\r\n].+[\r\n].+[\r\n](?P<url>[^\r\n]+)?')
urlpattern = requests.utils.re.compile(r'^(acestream|infohash)://[0-9a-f]{40}$|^(http|https)://.*.(acelive|acestream|acemedia|torrent)$')
for match in pattern.finditer(r.text, requests.auth.re.MULTILINE):
itemdict = match.groupdict()
name = itemdict.get('name', '').replace(' (allfon)','')
url = itemdict['url']
itemdict['logo'] = self.picons[name] = itemdict.get('logo', picons.logomap.get(name))
if requests.utils.re.search(urlpattern, url):
self.channels[name] = url
itemdict['url'] = quote(ensure_str(name),'')
self.playlist.addItem(itemdict)
m.update(ensure_binary(name))
self.etag = '"' + m.hexdigest() + '"'
logging.debug('[%s]: plugin playlist generated' % self.__class__.__name__)
self.playlisttime = gevent.time.time()
except requests.exceptions.RequestException:
logging.error("[%s]: can't download %s playlist!" % (self.__class__.__name__, config.url))
return False
except: logging.error(traceback.format_exc()); return False
return True
示例9: Playlistparser
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def Playlistparser(self):
try:
s = requests.Session()
s.mount('file://', FileAdapter())
with s.get(config.url, headers=self.headers, proxies=config.proxies, stream=False, timeout=30) as r:
if r.status_code != 304:
if r.encoding is None: r.encoding = 'utf-8'
self.headers['If-Modified-Since'] = gevent.time.strftime('%a, %d %b %Y %H:%M:%S %Z', gevent.time.gmtime(self.playlisttime))
self.playlist = PlaylistGenerator(m3uchanneltemplate=config.m3uchanneltemplate)
self.picons = picons.logomap
self.channels = {}
m = requests.auth.hashlib.md5()
logging.info('[%s]: playlist %s downloaded' % (self.__class__.__name__, config.url))
pattern = requests.utils.re.compile(r',(?P<name>.+) \((?P<group>.+)\)[\r\n]+(?P<url>[^\r\n]+)?')
urlpattern = requests.utils.re.compile(r'^(acestream|infohash)://[0-9a-f]{40}$|^(http|https)://.*.(acelive|acestream|acemedia|torrent)$')
for match in pattern.finditer(r.text, requests.auth.re.MULTILINE):
itemdict = match.groupdict()
name = itemdict.get('name', '')
itemdict['logo'] = self.picons[name] = itemdict.get('logo', picons.logomap.get(name))
url = itemdict['url']
if requests.utils.re.search(urlpattern, url):
self.channels[name] = url
itemdict['url'] = quote(ensure_str(name), '')
self.playlist.addItem(itemdict)
m.update(ensure_binary(name))
self.etag = '"' + m.hexdigest() + '"'
logging.debug('[%s]: plugin playlist generated' % self.__class__.__name__)
self.playlisttime = gevent.time.time()
except requests.exceptions.RequestException:
logging.error("[%s]: can't download %s playlist!" % (self.__class__.__name__, config.url))
return False
except: logging.error(traceback.format_exc()); return False
return True
示例10: request
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def request(self) -> "requests.model.Response":
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
import requests.exceptions
auth_map = {
"basic": HTTPBasicAuth,
"digest": HTTPDigestAuth,
}
session = requests.Session()
try:
from requests_file import FileAdapter
session.mount("file://", FileAdapter())
except ImportError:
pass
try:
reply = session.get(self._url, timeout=self._timeout)
# replace reply with an authenticated version if credentials are
# available and the server has requested authentication
if self._username and self._password and reply.status_code == 401:
auth_scheme = reply.headers["WWW-Authenticate"].split(" ")[0].lower()
if auth_scheme not in auth_map:
raise SevereCheckError(
"Unsupported authentication scheme {}".format(auth_scheme)
)
auth = auth_map[auth_scheme](self._username, self._password)
reply = session.get(self._url, timeout=self._timeout, auth=auth)
reply.raise_for_status()
return reply
except requests.exceptions.RequestException as error:
raise TemporaryCheckError(error) from error
示例11: async_get
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def async_get(event_loop):
""" AsyncSession cannot be created global since it will create
a different loop from pytest-asyncio. """
async_session = AsyncHTMLSession()
async_session.mount('file://', FileAdapter())
path = os.path.sep.join((os.path.dirname(os.path.abspath(__file__)), 'python.html'))
url = 'file://{}'.format(path)
return partial(async_session.get, url)
示例12: __init__
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def __init__(self, data, keys=None):
"""
:param dict data: The formatted repository data.
:param keys: The keys to use for verifying remote data.
:type keys: :py:class:`~king_phisher.security_keys.SecurityKeys`
"""
self.security_keys = keys or security_keys.SecurityKeys()
"""The :py:class:`~king_phisher.security_keys.SecurityKeys` used for verifying remote data."""
self._req_sess = requests.Session()
self._req_sess.mount('file://', requests_file.FileAdapter())
self.description = data.get('description')
self.homepage = data.get('homepage')
"""The URL of the homepage for this repository if it was specified."""
self.id = data['id']
"""The unique identifier of this repository."""
self.title = data['title']
"""The title string of this repository."""
self.url_base = data['url-base']
"""The base URL string of files included in this repository."""
self.collections = utilities.FreezableDict()
"""The dictionary of the different collection types included in this repository."""
if 'collections-include' in data:
# include-files is reversed so the dictionary can get .update()'ed and the first seen will be the value kept
for include in reversed(data['collections-include']):
include_data = self._fetch_json(include)
utilities.validate_json_schema(include_data, 'king-phisher.catalog.collections')
include_data = include_data['collections']
for collection_type in include.get('types', COLLECTION_TYPES):
collection = include_data.get(collection_type)
if collection is None:
continue
self._add_collection_data(collection_type, collection)
if 'collections' in data:
for collection_type in COLLECTION_TYPES:
collection = data['collections'].get(collection_type)
if collection is None:
continue
self._add_collection_data(collection_type, collection)
item_count = sum(len(collection) for collection in self.collections.values())
self.logger.debug("initialized catalog repository with {0} collection types and {1} total items".format(len(self.collections), item_count))
for collection_type, collection in self.collections.items():
collection.freeze()
self.collections[collection_type] = Collection(self, collection_type, collection)
self.collections.freeze()
示例13: Playlistparser
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def Playlistparser(self):
try:
s = requests.Session()
s.mount('file://', FileAdapter())
with s.get(config.url, headers=self.headers, proxies=config.proxies, stream=False, timeout=30) as playlist:
if playlist.status_code != 304:
if playlist.encoding is None: playlist.encoding = 'utf-8'
playlist = playlist.json()
self.headers['If-Modified-Since'] = gevent.time.strftime('%a, %d %b %Y %H:%M:%S %Z', gevent.time.gmtime(self.playlisttime))
self.playlist = PlaylistGenerator(m3uchanneltemplate=config.m3uchanneltemplate)
self.picons = picons.logomap
self.channels = {}
m = requests.auth.hashlib.md5()
logging.info('[%s]: playlist %s downloaded' % (self.__class__.__name__, config.url))
try:
urlpattern = requests.utils.re.compile(r'^(acestream|infohash)://[0-9a-f]{40}$|^(http|https)://.*.(acelive|acestream|acemedia|torrent)$')
for channel in playlist['channels']:
name = channel['name']
url = 'acestream://{url}'.format(**channel)
channel['group'] = channel.pop('cat')
channel['logo'] = self.picons[name] = channel.get('logo', picons.logomap.get(name))
if requests.utils.re.search(urlpattern, url):
self.channels[name] = url
channel['url'] = quote(ensure_str(name),'')
self.playlist.addItem(channel)
m.update(ensure_binary(name))
except Exception as e:
logging.error("[%s]: can't parse JSON! %s" % (self.__class__.__name__, repr(e)))
return False
self.etag = '"' + m.hexdigest() + '"'
logging.debug('[%s]: plugin playlist generated' % self.__class__.__name__)
self.playlisttime = gevent.time.time()
except requests.exceptions.RequestException:
logging.error("[%s]: can't download %s playlist!" % (self.__class__.__name__, config.url))
return False
except: logging.error(traceback.format_exc()); return False
示例14: Playlistparser
# 需要導入模塊: import requests_file [as 別名]
# 或者: from requests_file import FileAdapter [as 別名]
def Playlistparser(self):
try:
s = requests.Session()
s.mount('file://', FileAdapter())
with s.get(config.url, headers=self.headers, proxies=config.proxies, stream=False, timeout=30) as playlist:
if playlist.status_code != 304:
if playlist.encoding is None: playlist.encoding = 'utf-8'
playlist = playlist.json()
self.headers['If-Modified-Since'] = gevent.time.strftime('%a, %d %b %Y %H:%M:%S %Z', gevent.time.gmtime(self.playlisttime))
self.playlist = PlaylistGenerator(m3uchanneltemplate=config.m3uchanneltemplate)
self.picons = picons.logomap
self.channels = {}
m = requests.auth.hashlib.md5()
logging.info('[%s]: playlist %s downloaded' % (self.__class__.__name__, config.url))
try:
urlpattern = requests.utils.re.compile(r'^(acestream|infohash)://[0-9a-f]{40}$|^(http|https)://.*.(acelive|acestream|acemedia|torrent)$')
for channel in playlist['channels']:
name = channel['name']
url = 'infohash://{url}'.format(**channel)
channel['group'] = channel.pop('cat')
channel['logo'] = self.picons[name] = channel.get('logo', picons.logomap.get(name))
channel['tvgid'] = channel.pop('program')
if requests.utils.re.search(urlpattern, url):
self.channels[name] = url
channel['url'] = quote(ensure_str(name),'')
self.playlist.addItem(channel)
m.update(ensure_binary(name))
except Exception as e:
logging.error("[%s]: can't parse JSON! %s" % (self.__class__.__name__, repr(e)))
return False
self.etag = '"' + m.hexdigest() + '"'
logging.debug('[%s]: plugin playlist generated' % self.__class__.__name__)
self.playlisttime = gevent.time.time()
except requests.exceptions.RequestException:
logging.error("[%s]: can't download %s playlist!" % (self.__class__.__name__, config.url))
return False
except: logging.error(traceback.format_exc()); return False