fork(1) download
  1. import urllib.request
  2. from PIL import Image
  3.  
  4. class WebConn:
  5.  
  6. def __init__(self, url):
  7. self.url = url
  8.  
  9. def __enter__(self):
  10. self.logo = urllib.request.urlopen(url)
  11. return self.logo
  12.  
  13. def __exit__(self, exc_type, exc_val, exc_tb):
  14. print('exit')
  15.  
  16. if __name__ == '__main__':
  17. url = 'http://www.google.com/images/srpr/logo3w.png'
  18. file_name = 'image.jpg'
  19.  
  20. with WebConn(url) as im:
  21. f = open(file_name, "wb")
  22. f.write(im)
  23. f.close()
  24.  
  25. img = Image.open('./' + file_name)
  26. img.show()
Runtime error #stdin #stdout #stderr 0.06s 68032KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "/usr/lib/python3.5/urllib/request.py", line 1254, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.5/http/client.py", line 1107, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1152, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1103, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/lib/python3.5/http/client.py", line 849, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.5/socket.py", line 694, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./prog.py", line 20, in <module>
  File "./prog.py", line 10, in __enter__
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
  File "/usr/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 1282, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/lib/python3.5/urllib/request.py", line 1256, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>