本文整理汇总了Python中urllib.urlopen方法的典型用法代码示例。如果您正苦于以下问题:Python urllib.urlopen方法的具体用法?Python urllib.urlopen怎么用?Python urllib.urlopen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib
的用法示例。
在下文中一共展示了urllib.urlopen方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_sms_builtin
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def send_sms_builtin(self, recipient, sender=None):
"""
Sends SMS through built-in gateway
"""
if not settings.SMS_HTTP_URL:
raise ValueError(_('System is not configured for built-in SMS support.'))
if sender is None:
location = self.created_by.location
sender = location.title
data = urllib.urlencode({
'username' : settings.SMS_HTTP_USERNAME,
'password' : settings.SMS_HTTP_PASSWORD,
'numberto' : recipient.replace(' ', ''),
'numberfrom': sender.encode(SMS_ENCODING),
'message' : self.body.encode(SMS_ENCODING),
})
from ssl import _create_unverified_context
f = urllib.urlopen(settings.SMS_HTTP_URL, data, context=_create_unverified_context())
return f.read()
示例2: send
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def send(self):
pwhash = md5(self.conf['sms_http_password']).hexdigest()
params = {
'username' : self.conf['sms_http_user'],
'password' : pwhash,
'message' : self.body,
'from' : self.sender,
'to' : self.recipient,
}
if self.msg:
dlruri = settings.SERVO_URL + '/api/messages/?id={0}'.format(self.msg.code)
params['notify_url'] = dlruri
params = urllib.urlencode(params)
r = urllib.urlopen(self.URL, params, context=_create_unverified_context()).read()
if 'ERROR:' in r:
raise ValueError(self.ERRORS.get(r, _('Unknown error (%s)') % r))
示例3: download_dataset
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def download_dataset(path, source='https://www.cs.toronto.edu/~kriz/'
'cifar-10-python.tar.gz'):
"""
Downloads and extracts the dataset, if needed.
"""
files = ['data_batch_%d' % (i + 1) for i in range(5)] + ['test_batch']
for fn in files:
if not os.path.exists(os.path.join(path, 'cifar-10-batches-py', fn)):
break # at least one file is missing
else:
return # dataset is already complete
print("Downloading and extracting %s into %s..." % (source, path))
if sys.version_info[0] == 2:
from urllib import urlopen
else:
from urllib.request import urlopen
import tarfile
if not os.path.exists(path):
os.makedirs(path)
u = urlopen(source)
with tarfile.open(fileobj=u, mode='r|gz') as f:
f.extractall(path=path)
u.close()
示例4: hanlp_releases
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def hanlp_releases(cache=True):
global HANLP_RELEASES
if cache and HANLP_RELEASES:
return HANLP_RELEASES
# print('Request GitHub API')
req = urllib.Request('http://nlp.hankcs.com/download.php?file=version')
req.add_header('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36')
if PY == 3:
content = urllib.urlopen(req).read()
else:
content = urllib.urlopen(req).read()
content = json.loads(content.decode())
jar_version, jar_url, data_version, data_url = content
meta = [(jar_version, jar_url, data_version, data_url)]
HANLP_RELEASES = meta
return meta
示例5: run_simulation
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def run_simulation(self):
global TARGET, ADDR, VULN_VAR, TIMEOUT, REQ_TOTAL,\
METHOD, OTHER_VARIABLES
tmp = OTHER_VARIABLES
tmp[VULN_VAR] = self.genome
try:
if METHOD == 0:
prep = urllib.urlencode(tmp)
r = urllib.urlopen('http://%s/%s' % (TARGET, ADDR), data=prep, timeout=TIMEOUT)
else:
prep = urllib.urlencode(tmp)
req = urllib.Request('http://%s/%s' % (TARGET, ADDR), data=prep)
r = urllib.urlopen(req)
REQ_TOTAL += 1
self.m_text['text'] = r.get_data()
self.m_text['url'] = r.get_full_url()
self.m_text['status_code'] = r.getcode()
except:
pass
return self.m_text
示例6: read_file_or_url
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def read_file_or_url(self, fname):
# TODO: not working on localhost
if isinstance(fname, file):
result = open(fname, 'r')
else:
match = self.urlre.match(fname)
if match:
result = urllib.urlopen(match.group(1))
else:
fname = os.path.expanduser(fname)
try:
result = open(os.path.expanduser(fname), 'r')
except IOError:
result = open('%s.%s' % (os.path.expanduser(fname),
self.defaultExtension), 'r')
return result
示例7: loadData
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def loadData():
data_url = "data/multi-woz/data.json"
dataset_url = "https://www.repository.cam.ac.uk/bitstream/handle/1810/280608/MULTIWOZ2.zip?sequence=3&isAllowed=y"
if not os.path.exists("data"):
os.makedirs("data")
os.makedirs("data/multi-woz")
if not os.path.exists(data_url):
print("Downloading and unzipping the MultiWOZ dataset")
resp = urllib.urlopen(dataset_url)
zip_ref = ZipFile(BytesIO(resp.read()))
zip_ref.extractall("data/multi-woz")
zip_ref.close()
shutil.copy('data/multi-woz/MULTIWOZ2 2/data.json', 'data/multi-woz/')
shutil.copy('data/multi-woz/MULTIWOZ2 2/valListFile.json', 'data/multi-woz/')
shutil.copy('data/multi-woz/MULTIWOZ2 2/testListFile.json', 'data/multi-woz/')
shutil.copy('data/multi-woz/MULTIWOZ2 2/dialogue_acts.json', 'data/multi-woz/')
示例8: test_distribute
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def test_distribute(self):
real_urlopen = urllib.urlopen
real_server_proxy = xmlrpclib.ServerProxy
try:
xmlrpclib.ServerProxy = ProxyStub(
"distributedata.py", xmlrpclib.ServerProxy, False
)
urllib.urlopen = urlopenstub
data = pypidata.get_data("distribute")
rating = rate(data)
self.assertEqual(
rating,
(
9,
[
"The classifiers should specify what minor versions of Python "
"you support as well as what major version.",
"You should have three or more owners of the project on PyPI.",
],
),
)
finally:
xmlrpclib.ServerProxy = real_server_proxy
urllib.urlopen = real_urlopen
示例9: get_remote_info_json
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def get_remote_info_json(jfname):
try:
logging.info('loading example '+jfname)
rfo = urllib.urlopen(jfname)
di = json.loads(rfo.read())
nat, glbs = 0, 0
for k,v in di.items():
if k != 'dimensions' or k != 'variables':
glbs +=1
for k,v in di['variables'].items():
for a in v: nat += 1
dims = [ l for k, v in di['dimensions'].items() for d, l in v.items() if d == 'length' ]
return { 'num global attr' : glbs, 'num vars' : len(di['variables'].keys()), 'num dims' : \
len(di['dimensions'].keys()), 'ave attrs per var' : nat / len(di['variables'].keys()), \
'dims sizes' : dims }
except Exception, e:
logging.warn("WARN get_remote_info_json on %s : %s, update S3 bucket" % (jfname, str(e)))
return {}
#---------------------------------------------------------------------------------
示例10: getPage
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def getPage( self, event ):
"""Parse URL, add addressing scheme and retrieve file"""
# parse the URL
myURL = event.widget.get()
components = urlparse.urlparse( myURL )
self.contents.text_state = NORMAL
# if addressing scheme not specified, use http
if components[ 0 ] == "":
myURL = "http://" + myURL
# connect and retrieve the file
try:
tempFile = urllib.urlopen( myURL )
self.contents.settext( tempFile.read() ) # show results
tempFile.close()
except IOError:
self.contents.settext( "Error finding file" )
self.contents.text_state = DISABLED
示例11: download_file
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def download_file(url, destination=''):
if not destination:
fd, destination = tempfile.mkstemp()
os.remove(destination)
os.close(fd)
if not os.path.isfile(destination):
ctx.logger.info('Downloading {0} to {1}...'.format(
url, destination))
try:
final_url = urllib.urlopen(url).geturl()
if final_url != url:
ctx.logger.debug('Redirected to {0}'.format(final_url))
f = urllib.URLopener()
# TODO: try except with @retry
f.retrieve(final_url, destination)
except Exception:
curl_download_with_retries(url, destination)
else:
ctx.logger.debug('File {0} already exists...'.format(destination))
return destination
示例12: http_request
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def http_request(url,
data=None,
method='PUT',
headers=None,
timeout=None,
should_fail=False):
headers = headers or {}
request = urllib2.Request(url, data=data, headers=headers)
request.get_method = lambda: method
try:
if timeout:
return urllib2.urlopen(request, timeout=timeout)
return urllib2.urlopen(request)
except urllib2.URLError as e:
if not should_fail:
ctx.logger.error('Failed to {0} {1} (reason: {2})'.format(
method, url, e.reason))
示例13: scanurl
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def scanurl(self, link):
url = self.api_url + "url/scan"
parameters = {"url": link, "apikey": self.api_key}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
try:
response = urllib2.urlopen(req)
xjson = response.read()
response_code = json.loads(xjson).get('response_code')
verbose_msg = json.loads(xjson).get('verbose_msg')
if response_code == 1:
print verbose_msg
return xjson
else:
print verbose_msg
except urllib2.HTTPError, e:
self.handleHTTPErros(e.code)
示例14: getfile
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def getfile(self, file):
if os.path.isfile(file):
f = open(file, 'rb')
file = hashlib.sha256(f.read()).hexdigest()
f.close()
url = self.api_url + "file/report"
parameters = {"resource": file, "apikey": self.api_key}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
try:
response = urllib2.urlopen(req)
xjson = response.read()
response_code = json.loads(xjson).get('response_code')
verbose_msg = json.loads(xjson).get('verbose_msg')
if response_code == 1:
print verbose_msg
return self.report(xjson)
else:
print verbose_msg
except urllib2.HTTPError, e:
self.handleHTTPErros(e.code)
示例15: geturl
# 需要导入模块: import urllib [as 别名]
# 或者: from urllib import urlopen [as 别名]
def geturl(self, resource):
url = self.api_url + "url/report"
parameters = {"resource": resource, "apikey": self.api_key}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
try:
response = urllib2.urlopen(req)
xjson = response.read()
response_code = json.loads(xjson).get('response_code')
verbose_msg = json.loads(xjson).get('verbose_msg')
if response_code == 1:
print verbose_msg
return self.report(xjson)
else:
print verbose_msg
except urllib2.HTTPError, e:
self.handleHTTPErros(e.code)