0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

プロキシあるJupyter環境で外部データを取ってくる

Posted at

やりたいこと

会社のプロキシがある環境で、Kerasデータセットのミニストを取りたかった。

import tensorflow as tf

# MNISTデータセットをロード
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

エラー発生

/usr/lib/python3.6/http/client.py in _get_hostport(self, host, port)
    904                 try:
--> 905                     port = int(host[i+1:])
    906                 except ValueError:

ValueError: invalid literal for int() with base 10: 'port'

During handling of the above exception, another exception occurred:

InvalidURL                                Traceback (most recent call last)
<ipython-input-8-8d03e8f41e48> in <module>
     17     include_top = False,
     18     weights = "imagenet",
---> 19     input_shape = None)
     20 
     21     x = base_model.output

/usr/local/lib/python3.6/dist-packages/keras/applications/xception.py in Xception(include_top, weights, input_tensor, input_shape, pooling, classes, classifier_activation)
    307           TF_WEIGHTS_PATH_NO_TOP,
    308           cache_subdir='models',
--> 309           file_hash='b0042744bf5b25fce3cb969f33bebb97')
    310     model.load_weights(weights_path)
    311   elif weights is not None:

/usr/local/lib/python3.6/dist-packages/keras/utils/data_utils.py in get_file(fname, origin, untar, md5_hash, file_hash, cache_subdir, hash_algorithm, extract, archive_format, cache_dir)
    272     try:
    273       try:
--> 274         urlretrieve(origin, fpath, dl_progress)
    275       except urllib.error.HTTPError as e:
    276         raise Exception(error_msg.format(origin, e.code, e.msg))

/usr/local/lib/python3.6/dist-packages/keras/utils/data_utils.py in urlretrieve(url, filename, reporthook, data)
     80           break
     81 
---> 82     response = urlopen(url, data)
     83     with open(filename, 'wb') as fd:
     84       for chunk in chunk_read(response, reporthook=reporthook):

/usr/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221     else:
    222         opener = _opener
--> 223     return opener.open(url, data, timeout)
    224 
    225 def install_opener(opener):

/usr/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
    524             req = meth(req)
    525 
--> 526         response = self._open(req, data)
    527 
    528         # post-process response

/usr/lib/python3.6/urllib/request.py in _open(self, req, data)
    542         protocol = req.type
    543         result = self._call_chain(self.handle_open, protocol, protocol +
--> 544                                   '_open', req)
    545         if result:
    546             return result

/usr/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502         for handler in handlers:
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:
    506                 return result

/usr/lib/python3.6/urllib/request.py in https_open(self, req)
   1366         def https_open(self, req):
   1367             return self.do_open(http.client.HTTPSConnection, req,
-> 1368                 context=self._context, check_hostname=self._check_hostname)
   1369 
   1370         https_request = AbstractHTTPHandler.do_request_

/usr/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1291 
   1292         # will parse host:port
-> 1293         h = http_class(host, timeout=req.timeout, **http_conn_args)
   1294         h.set_debuglevel(self._debuglevel)
   1295 

/usr/lib/python3.6/http/client.py in __init__(self, host, port, key_file, cert_file, timeout, source_address, context, check_hostname)
   1411                      check_hostname=None):
   1412             super(HTTPSConnection, self).__init__(host, port, timeout,
-> 1413                                                   source_address)
   1414             if (key_file is not None or cert_file is not None or
   1415                         check_hostname is not None):

/usr/lib/python3.6/http/client.py in __init__(self, host, port, timeout, source_address)
    865         self._tunnel_headers = {}
    866 
--> 867         (self.host, self.port) = self._get_hostport(host, port)
    868 
    869         self._validate_host(self.host)

/usr/lib/python3.6/http/client.py in _get_hostport(self, host, port)
    908                         port = self.default_port
    909                     else:
--> 910                         raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
    911                 host = host[:i]
    912             else:

InvalidURL: nonnumeric port: 'port' 

解決方法

環境に設定してあげれば解決。

import os

os.environ["HTTP_PROXY"] = "http://10.13x.x.6:80xx"
os.environ["HTTPS_PROXY"] = "http://10.13x.x.6:80xx"

os.environ["https_proxy"] = "http://10.13x.x.6:80xx"
os.environ["http_proxy"] = "http://10.13x.x.6:80xx"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?