title: getting error (ConnectionRefusedError: [Errno 61] Connection refused)
body: <p>I am trying to run this basic HTTP proxy, but I'm getting this error. It claims there are two exceptions but they both say: [Errno 61] Connection refused. I can't see what line is causing the first exception, but the second exception is being caused by the line: self.copyfile(urllib.request.urlopen(url), self.wfile)</p>
  <p>So, I see that the connection is faulted in some way, but I'm not sure what code needs to be changed to fix that.</p>
  <p>Thank you for the help!</p>
  <pre><code>import http.server
  import socketserver
  import urllib.request
  
  
  class MyProxy(http.server.SimpleHTTPRequestHandler):
  
      def do_GET(self):
          print(self.path)
          url = self.path
          self.send_response(200)
          self.end_headers()
          self.copyfile(urllib.request.urlopen(url), self.wfile)
  
  
  
  
  
  # --- main ---
  def main():
  
      PORT = 8888
      local_ip = &quot;127.0.0.1&quot;
  
      httpd = None
  
      try:
          socketserver.TCPServer.allow_reuse_address = True   # solution for `OSError: [Errno 98] Address already in use`
          httpd = socketserver.TCPServer((local_ip, PORT), MyProxy)
          print(f&quot;{local_ip}:{PORT}&quot;)
          httpd.serve_forever()
      except KeyboardInterrupt:
          print(&quot;Pressed Ctrl+C&quot;)
      finally:
          if httpd:
              httpd.shutdown()
              #httpd.socket.close()
  
  
  if __name__ == '__main__':
      main()
  
  
  Exception happened during processing of request from ('127.0.0.1', 49700)
  Traceback (most recent call last):
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 1319, in do_open
      h.request(req.get_method(), req.selector, req.data, headers,
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 1245, in request
      self._send_request(method, url, body, headers, encode_chunked)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 1291, in _send_request
      self.endheaders(body, encode_chunked=encode_chunked)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 1240, in endheaders
      self._send_output(message_body, encode_chunked=encode_chunked)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 1008, in _send_output
      self.send(msg)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 948, in send
      self.connect()
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 919, in connect
      self.sock = self._create_connection(
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socket.py&quot;, line 808, in create_connection
      raise err
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socket.py&quot;, line 796, in create_connection
      sock.connect(sa)
  ConnectionRefusedError: [Errno 61] Connection refused
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 316, in _handle_request_noblock
      self.process_request(request, client_address)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 347, in process_request
      self.finish_request(request, client_address)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 360, in finish_request
      self.RequestHandlerClass(request, client_address, self)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/server.py&quot;, line 647, in __init__
      super().__init__(*args, **kwargs)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 720, in __init__
      self.handle()
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/server.py&quot;, line 427, in handle
      self.handle_one_request()
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/server.py&quot;, line 415, in handle_one_request
      method()
    File &quot;/Users/jalen/PycharmProjects/HW4/proxy.py&quot;, line 13, in do_GET
      self.copyfile(urllib.request.urlopen(url), self.wfile)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 222, in urlopen
      return opener.open(url, data, timeout)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 525, in open
      response = self._open(req, data)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 542, in _open
      result = self._call_chain(self.handle_open, protocol, protocol +
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 502, in _call_chain
      result = func(*args)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 1348, in http_open
      return self.do_open(http.client.HTTPConnection, req)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 1322, in do_open
      raise URLError(err)
  urllib.error.URLError: &lt;urlopen error [Errno 61] Connection refused&gt;
  ----------------------------------------
  http://www.example.com/
  127.0.0.1 - - [12/Apr/2022 22:51:29] &quot;GET http://www.example.com/ HTTP/1.1&quot; 200 -
  ----------------------------------------
  Exception happened during processing of request from ('127.0.0.1', 49702)
  Traceback (most recent call last):
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 1319, in do_open
      h.request(req.get_method(), req.selector, req.data, headers,
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 1245, in request
      self._send_request(method, url, body, headers, encode_chunked)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 1291, in _send_request
      self.endheaders(body, encode_chunked=encode_chunked)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 1240, in endheaders
      self._send_output(message_body, encode_chunked=encode_chunked)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 1008, in _send_output
      self.send(msg)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 948, in send
      self.connect()
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/client.py&quot;, line 919, in connect
      self.sock = self._create_connection(
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socket.py&quot;, line 808, in create_connection
      raise err
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socket.py&quot;, line 796, in create_connection
      sock.connect(sa)
  ConnectionRefusedError: [Errno 61] Connection refused
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 316, in _handle_request_noblock
      self.process_request(request, client_address)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 347, in process_request
      self.finish_request(request, client_address)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 360, in finish_request
      self.RequestHandlerClass(request, client_address, self)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/server.py&quot;, line 647, in __init__
      super().__init__(*args, **kwargs)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socketserver.py&quot;, line 720, in __init__
      self.handle()
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/server.py&quot;, line 427, in handle
      self.handle_one_request()
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/http/server.py&quot;, line 415, in handle_one_request
      method()
    File &quot;/Users/jalen/PycharmProjects/HW4/proxy.py&quot;, line 13, in do_GET
      self.copyfile(urllib.request.urlopen(url), self.wfile)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 222, in urlopen
      return opener.open(url, data, timeout)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 525, in open
      response = self._open(req, data)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 542, in _open
      result = self._call_chain(self.handle_open, protocol, protocol +
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 502, in _call_chain
      result = func(*args)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 1348, in http_open
      return self.do_open(http.client.HTTPConnection, req)
    File &quot;/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/urllib/request.py&quot;, line 1322, in do_open
      raise URLError(err)
  urllib.error.URLError: &lt;urlopen error [Errno 61] Connection refused&gt;
  </code></pre>