本文整理匯總了Python中six.moves.urllib_request.urlopen方法的典型用法代碼示例。如果您正苦於以下問題:Python urllib_request.urlopen方法的具體用法?Python urllib_request.urlopen怎麽用?Python urllib_request.urlopen使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類six.moves.urllib_request
的用法示例。
在下文中一共展示了urllib_request.urlopen方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: http_HEAD
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def http_HEAD(self, url, headers={}):
"""
Perform an HTTP HEAD request.
Args:
url (str): The URL to GET.
Kwargs:
headers (dict): A dictionary describing any headers you would like
to add to the request. (eg. ``{'X-Test': 'testing'}``)
Returns:
An :class:`HttpResponse` object containing headers and other
meta-information about the page.
"""
request = urllib_request.Request(url)
request.get_method = lambda: 'HEAD'
request.add_header('User-Agent', self._user_agent)
for key in headers:
request.add_header(key, headers[key])
response = urllib_request.urlopen(request)
return HttpResponse(response)
示例2: http_DELETE
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def http_DELETE(self, url, headers={}):
"""
Perform an HTTP DELETE request.
Args:
url (str): The URL to GET.
Kwargs:
headers (dict): A dictionary describing any headers you would like
to add to the request. (eg. ``{'X-Test': 'testing'}``)
Returns:
An :class:`HttpResponse` object containing headers and other
meta-information about the page.
"""
request = urllib_request.Request(url)
request.get_method = lambda: 'DELETE'
request.add_header('User-Agent', self._user_agent)
for key in headers:
request.add_header(key, headers[key])
response = urllib_request.urlopen(request)
return HttpResponse(response)
示例3: _parse_redirect
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def _parse_redirect(self, url, hdrs={}):
class NoRedirection(urllib_request.HTTPErrorProcessor):
def http_response(self, request, response):
return response
opener = urllib_request.build_opener(NoRedirection)
urllib_request.install_opener(opener) # @ big change
request = urllib_request.Request(url, headers=hdrs)
try:
response = urllib_request.urlopen(request)
except urllib_error.HTTPError as e:
if e.code == 429 or e.code == 403:
msg = 'Daily view limit reached'
common.kodi.notify(header=None, msg=msg, duration=3000)
raise ResolverError(msg)
response_headers = dict([(item[0].title(), item[1]) for item in list(response.info().items())])
cookie = response_headers.get('Set-Cookie', None)
if cookie:
self.headers.update({'Cookie': cookie})
return response.geturl()
示例4: create_driver
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def create_driver():
global driver, browser
driver = None
browser = local.DEFAULT_BROWSER
if browser == "chrome":
try:
urllib_request.urlopen("http://127.0.0.1:9222/json")
except urllib_error.URLError:
print("Unable to start WebDriver, Chrome is not responding.")
return
chrome_options = webdriver.chrome.options.Options()
chrome_options.experimental_options["debuggerAddress"] = "127.0.0.1:9222"
driver = webdriver.Chrome(local.CHROME_DRIVER_PATH, chrome_options=chrome_options)
elif browser == "firefox":
driver = marionette_driver.marionette.Marionette()
else:
print("Unknown browser: " + browser)
browser = None
示例5: switch_to_active_tab
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def switch_to_active_tab():
if browser == "chrome":
tabs = json.load(urllib_request.urlopen("http://127.0.0.1:9222/json"))
# Chrome seems to order the tabs by when they were last updated, so we find
# the first one that is not an extension.
for tab in tabs:
if not tab["url"].startswith("chrome-extension://"):
active_tab = tab["id"]
break
for window in driver.window_handles:
# ChromeDriver adds to the raw ID, so we just look for substring match.
if active_tab in window:
driver.switch_to_window(window);
print("Switched Chrome to: " + driver.title.encode('ascii', 'backslashreplace'))
return
print("Did not find active tab in Chrome.")
elif browser == "firefox":
driver.delete_session()
driver.start_session()
示例6: _update_opener
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def _update_opener(self):
"""
Builds and installs a new opener to be used by all future calls to
:func:`urllib2.urlopen`.
"""
handlers = [urllib_request.HTTPCookieProcessor(self._cj), urllib_request.HTTPBasicAuthHandler()]
if self._http_debug:
handlers += [urllib_request.HTTPHandler(debuglevel=1)]
else:
handlers += [urllib_request.HTTPHandler()]
if self._proxy:
handlers += [urllib_request.ProxyHandler({'http': self._proxy})]
try:
import platform
node = platform.node().lower()
except:
node = ''
if not self._ssl_verify or node == 'xboxone':
try:
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
if self._http_debug:
handlers += [urllib_request.HTTPSHandler(context=ctx, debuglevel=1)]
else:
handlers += [urllib_request.HTTPSHandler(context=ctx)]
except:
pass
opener = urllib_request.build_opener(*handlers)
urllib_request.install_opener(opener)
示例7: _fetch
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def _fetch(self, url, form_data={}, headers={}, compression=True):
"""
Perform an HTTP GET or POST request.
Args:
url (str): The URL to GET or POST.
form_data (dict): A dictionary of form data to POST. If empty, the
request will be a GET, if it contains form data it will be a POST.
Kwargs:
headers (dict): A dictionary describing any headers you would like
to add to the request. (eg. ``{'X-Test': 'testing'}``)
compression (bool): If ``True`` (default), try to use gzip
compression.
Returns:
An :class:`HttpResponse` object containing headers and other
meta-information about the page and the page content.
"""
req = urllib_request.Request(url)
if form_data:
if isinstance(form_data, six.string_types):
form_data = form_data
else:
form_data = urllib_parse.urlencode(form_data, True)
form_data = form_data.encode('utf-8') if six.PY3 else form_data
req = urllib_request.Request(url, form_data)
req.add_header('User-Agent', self._user_agent)
for key in headers:
req.add_header(key, headers[key])
if compression:
req.add_header('Accept-Encoding', 'gzip')
host = req.host if six.PY3 else req.get_host()
req.add_unredirected_header('Host', host)
response = urllib_request.urlopen(req, timeout=15)
return HttpResponse(response)
示例8: __init__
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def __init__(self, response):
"""
Args:
response (:class:`mimetools.Message`): The object returned by a call
to :func:`urllib2.urlopen`.
"""
self._response = response
示例9: get_media_url
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def get_media_url(self, host, media_id):
web_url = self.get_url(host, media_id)
logger.log_debug('HugeFiles: get_link: %s' % (web_url))
html = self.net.http_GET(web_url).content
r = re.findall('File Not Found', html)
if r:
raise ResolverError('File Not Found or removed')
# Grab data values
data = helpers.get_hidden(html)
data.update(captcha_lib.do_captcha(html))
logger.log_debug('HugeFiles - Requesting POST URL: %s with data: %s' % (web_url, data))
html = self.net.http_POST(web_url, data).content
# Re-grab data values
data = helpers.get_hidden(html)
data['referer'] = web_url
headers = {'User-Agent': common.EDGE_USER_AGENT}
logger.log_debug('HugeFiles - Requesting POST URL: %s with data: %s' % (web_url, data))
request = urllib_request.Request(web_url, data=urllib_parse.urlencode(data), headers=headers)
try:
stream_url = urllib_request.urlopen(request).geturl()
except:
return
logger.log_debug('Hugefiles stream Found: %s' % stream_url)
return stream_url
示例10: _fetchIP
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def _fetchIP(self, url):
try:
with contextlib.closing(urlopen(url, timeout=2)) as f:
if f.getcode() == 200:
content = f.read().decode().strip()[:40]
for c in content:
if c not in VALID_IP_CHARACTERS:
raise ValueError("Public IP response included invalid character '{}'.".format(c))
return content
except Exception:
logging.debug('Could not refresh public IP from {}'.format(url), exc_info=True)
return None
示例11: validate
# 需要導入模塊: from six.moves import urllib_request [as 別名]
# 或者: from six.moves.urllib_request import urlopen [as 別名]
def validate(ticket):
"""
Will attempt to validate the ticket. If validation fails, then False
is returned. If validation is successful, then True is returned
and the validated username is saved in the session under the
key `CAS_USERNAME_SESSION_KEY`.
"""
cas_username_session_key = current_app.config['CAS_USERNAME_SESSION_KEY']
current_app.logger.debug("validating token {0}".format(ticket))
cas_validate_url = create_cas_validate_url(
current_app.config['CAS_VALIDATE_SERVER'],
current_app.config['CAS_VALIDATE_ROUTE'],
url_for('cas.login', _external=True),
ticket)
current_app.logger.debug("Making GET request to {0}".format(cas_validate_url))
try:
response = urlopen(cas_validate_url).read()
ticketid = _parse_tag(response, "cas:user")
strs = [s.strip() for s in ticketid.split('|') if s.strip()]
username, is_valid = None, False
if len(strs) == 1:
username = strs[0]
is_valid = True
user_info = json.loads(_parse_tag(response, "cas:other"))
current_app.logger.info(user_info)
except ValueError:
current_app.logger.error("CAS returned unexpected result")
is_valid = False
return is_valid
if is_valid:
current_app.logger.debug("valid")
session[cas_username_session_key] = username
user = UserCache.get(username)
session["acl"] = dict(uid=user_info.get("uuid"),
avatar=user.avatar if user else user_info.get("avatar"),
userId=user_info.get("id"),
userName=user_info.get("name"),
nickName=user_info.get("nickname"),
parentRoles=user_info.get("parents"),
childRoles=user_info.get("children"),
roleName=user_info.get("role"))
session["uid"] = user_info.get("uuid")
current_app.logger.debug(session)
current_app.logger.debug(request.url)
else:
current_app.logger.debug("invalid")
return is_valid