本文整理汇总了Python中future.moves.urllib.request.urlopen方法的典型用法代码示例。如果您正苦于以下问题:Python request.urlopen方法的具体用法?Python request.urlopen怎么用?Python request.urlopen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类future.moves.urllib.request
的用法示例。
在下文中一共展示了request.urlopen方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_old_urllib_import
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_old_urllib_import(self):
"""
Tests whether an imported module can import the old urllib package.
Importing future.standard_library in a script should be possible and
not disrupt any uses of the old Py2 standard library names in modules
imported by that script.
"""
code1 = '''
from future import standard_library
with standard_library.suspend_hooks():
import module_importing_old_urllib
'''
self._write_test_script(code1, 'runme.py')
code2 = '''
import urllib
assert 'urlopen' in dir(urllib)
print('Import succeeded!')
'''
self._write_test_script(code2, 'module_importing_old_urllib.py')
output = self._run_test_script('runme.py')
print(output)
self.assertTrue(True)
示例2: test_bad_address
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_bad_address(self):
# Make sure proper exception is raised when connecting to a bogus
# address.
bogus_domain = "sadflkjsasf.i.nvali.d"
try:
socket.gethostbyname(bogus_domain)
except (OSError, socket.error): # for Py3 and Py2 respectively
# socket.gaierror is too narrow, since getaddrinfo() may also
# fail with EAI_SYSTEM and ETIMEDOUT (seen on Ubuntu 13.04),
# i.e. Python's TimeoutError.
pass
else:
# This happens with some overzealous DNS providers such as OpenDNS
self.skipTest("%r should not resolve for test to work" % bogus_domain)
self.assertRaises(IOError,
# SF patch 809915: In Sep 2003, VeriSign started
# highjacking invalid .com and .net addresses to
# boost traffic to their own site. This test
# started failing then. One hopes the .invalid
# domain will be spared to serve its defined
# purpose.
# urllib.urlopen, "http://www.sadflkjsasadf.com/")
urllib_request.urlopen,
"http://sadflkjsasf.i.nvali.d/")
示例3: test_register_payout
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_register_payout(self):
client = api.Client(url=url, config_path=tempfile.mktemp())
config = client.config()
self.assertTrue(client.register())
result = json.loads(
urlopen(url + '/api/online/json').read().decode('utf8')
)
result = [farmer for farmer in result['farmers']
if farmer['payout_addr'] == config['payout_address']]
last_seen = result[0]['last_seen']
reg_time = result[0]['reg_time']
result = json.dumps(result, sort_keys=True)
expected = json.dumps([{
'height': 0,
'nodeid': common.address2nodeid(config['payout_address']),
'last_seen': last_seen,
'payout_addr': config['payout_address'],
'reg_time': reg_time,
'bandwidth_upload': 0,
'bandwidth_download': 0,
"ip": "",
'uptime': 100.0
}], sort_keys=True)
self.assertEqual(result, expected)
示例4: is_port_forwarded
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def is_port_forwarded(source_ip, port, proto, forwarding_servers):
global true_socket
if source_ip is not None:
socket.socket = build_bound_socket(source_ip)
ret = 0
for forwarding_server in forwarding_servers:
url = "http://" + forwarding_server["addr"] + ":"
url += str(forwarding_server["port"])
url += forwarding_server["url"]
url += "?action=is_port_forwarded&port=" + str(port)
url += "&proto=" + str(proto.upper())
try:
r = urlopen(url, timeout=2)
response = r.read().decode("utf-8")
if "yes" in response:
ret = 1
break
except:
continue
socket.socket = true_socket
return ret
示例5: download
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def download(name, url, size, target, step=None):
with tqdm(
bar_format="{l_bar}{bar}| [{remaining}, {rate_fmt}]",
desc="{:<18}".format(name),
leave=False,
total=size,
unit="B",
unit_scale=True,
) as bar:
with target.open("wb", ensure=True) as fh:
url_request = Request(url)
with contextlib.closing(urlopen(url_request)) as socket:
while True:
block = socket.read(4096)
if not block:
break
fh.write(block)
bar.update(len(block))
if step:
ProgressOverall.done_part(step, bar.n / size)
if bar.n != size:
raise RuntimeError(
"Error downloading %s: received %d bytes, expected %d"
% (url, bar.n, size)
)
示例6: test_urllib_request_ssl_redirect
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_urllib_request_ssl_redirect(self):
"""
This site redirects to https://...
It therefore requires ssl support.
"""
import future.moves.urllib.request as urllib_request
from pprint import pprint
URL = 'http://pypi.python.org/pypi/{0}/json'
package = 'future'
r = urllib_request.urlopen(URL.format(package))
# pprint(r.read().decode('utf-8'))
self.assertTrue(True)
示例7: test_urllib_request_http
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_urllib_request_http(self):
"""
This site (python-future.org) uses plain http (as of 2014-09-23).
"""
import future.moves.urllib.request as urllib_request
from pprint import pprint
URL = 'http://python-future.org'
r = urllib_request.urlopen(URL)
data = r.read()
self.assertTrue(b'</html>' in data)
示例8: test_install_aliases
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_install_aliases(self):
"""
Does the install_aliases() interface monkey-patch urllib etc. successfully?
"""
from future.standard_library import remove_hooks, install_aliases
remove_hooks()
install_aliases()
from collections import Counter, OrderedDict # backported to Py2.6
from collections import UserDict, UserList, UserString
# Requires Python dbm support:
# import dbm
# import dbm.dumb
# import dbm.gnu
# import dbm.ndbm
from itertools import filterfalse, zip_longest
from subprocess import check_output # backported to Py2.6
from subprocess import getoutput, getstatusoutput
from sys import intern
# test_support may not be available (e.g. on Anaconda Py2.6):
# import test.support
import urllib.error
import urllib.parse
import urllib.request
import urllib.response
import urllib.robotparser
self.assertTrue('urlopen' in dir(urllib.request))
示例9: test_future_moves_urllib_request
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_future_moves_urllib_request(self):
from future.moves.urllib import request as urllib_request
functions = ['getproxies',
'pathname2url',
'proxy_bypass',
'quote',
'request_host',
'splitattr',
'splithost',
'splitpasswd',
'splitport',
'splitquery',
'splittag',
'splittype',
'splituser',
'splitvalue',
'thishost',
'to_bytes',
'unquote',
# 'unquote_to_bytes', # Is there an equivalent in the Py2 stdlib?
'unwrap',
'url2pathname',
'urlcleanup',
'urljoin',
'urlopen',
'urlparse',
'urlretrieve',
'urlsplit',
'urlunparse']
self.assertTrue(all(fn in dir(urllib_request) for fn in functions))
示例10: urlopen
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def urlopen(self, *args, **kwargs):
resource = args[0]
with support.transient_internet(resource):
r = urllib_request.urlopen(*args, **kwargs)
try:
yield r
finally:
r.close()
示例11: test_basic
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_basic(self):
# Simple test expected to pass.
with self.urlopen("http://www.python.org/") as open_url:
for attr in ("read", "readline", "readlines", "fileno", "close",
"info", "geturl"):
self.assertTrue(hasattr(open_url, attr), "object returned from "
"urlopen lacks the %s attribute" % attr)
self.assertTrue(open_url.read(), "calling 'read' failed")
示例12: test_readlines
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_readlines(self):
# Test both readline and readlines.
with self.urlopen("http://www.python.org/") as open_url:
self.assertIsInstance(open_url.readline(), bytes,
"readline did not return a string")
self.assertIsInstance(open_url.readlines(), list,
"readlines did not return a list")
示例13: test_info
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_info(self):
# Test 'info'.
with self.urlopen("http://www.python.org/") as open_url:
info_obj = open_url.info()
self.assertIsInstance(info_obj, email_message.Message,
"object returned by 'info' is not an "
"instance of email_message.Message")
self.assertEqual(info_obj.get_content_subtype(), "html")
示例14: test_geturl
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def test_geturl(self):
# Make sure same URL as opened is returned by geturl.
URL = "https://www.python.org/" # EJS: changed recently from http:// ?!
with self.urlopen(URL) as open_url:
gotten_url = open_url.geturl()
self.assertEqual(gotten_url, URL)
示例15: granule_preview_image
# 需要导入模块: from future.moves.urllib import request [as 别名]
# 或者: from future.moves.urllib.request import urlopen [as 别名]
def granule_preview_image(self, dataset_id, granule, year, day, variable, path=''):
''' Granule Preview Image Service provides thumbnail image of selected variable for\
selected granule.
:param dataset_id: Search granules belong to given PODAAC Dataset persistent ID.
:type dataset_id: :mod:`string`
:param granule: string granule name.
:type granule: :mod:`string`
:param year: year in 4 digits. Example= '2014'
:type year: :mod:`string`
:param day: day of year in 3 digits. Example= '140'
:type day: :mod:`string`
:param variable_id: Variable id described in dataset variable service.
:type variable_id: :mod:`string`
:returns: returns thumbnail image of selected variable for selected granule.
'''
try:
url = self.URL + 'preview/' + dataset_id + '/' + year + \
'/' + day + '/' + granule + '/' + variable + '.png'
if path:
path = path + '/' + dataset_id + '.png'
else:
path = os.path.join(os.path.dirname(
__file__), dataset_id + '.png')
with open(path, 'wb') as image_file:
image = urlopen(url)
image_file.write(image.read())
except Exception as e:
print(e)
raise
return image