Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't understand [Errno 111] Connection refused

I'm sure this is a very simple question (HTTP newbie) but I couldn't find the answer myself. I would appreciate any help here.

I have a web server:

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(), 8000))
serversocket.listen(10)

while True:
    print("waiting...")
    conn = serversocket.accept()
    data = conn[0].recv(1024)
    print(data) 

I also have a client trying to send a GET request:

import requests 
URL = "http://localhost:8000/api/v1/a?b=2"
r = requests.get(url = URL)

In this stage I don't want to do anything with the request, just to make sure I receive it, but this fails...

I run:

  • python3 server.py &
  • python3 client.py

and got:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 562, in urlopen
    body=body, headers=headers)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.4/http/client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.4/http/client.py", line 1126, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.4/http/client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.4/http/client.py", line 922, in _send_output
    self.send(msg)
  File "/usr/lib/python3.4/http/client.py", line 857, in send
    self.connect()
  File "/usr/lib/python3.4/http/client.py", line 834, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python3.4/socket.py", line 512, in create_connection
    raise err
  File "/usr/lib/python3.4/socket.py", line 503, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 330, in send
    timeout=timeout
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 612, in urlopen
    raise MaxRetryError(self, url, e)
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /api/v1/similar?word=apple (Caused by <class 'ConnectionRefusedError'>: [Errno 111] Connection refused)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "client.py", line 5, in <module>
    r = requests.get(url = URL)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 455, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 558, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 378, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /api/v1/similar?word=apple (Caused by <class 'ConnectionRefusedError'>: [Errno 111] Connection refused)
like image 853
CIsForCookies Avatar asked Oct 03 '18 12:10

CIsForCookies


1 Answers

Connection refused is generated by the kernel because receiving side refuses to establish TCP connection. Possible reasons are:

  1. No on listen on this host/port
  2. iptables (or other firewall) specifically -j REJECT those connections.

From your code it's hard to say if you listen on a proper host/port or not.

How to debug?

  1. Run you service. Without connecting to it run 'netstat -lnpt', it should provide you with list of services listening on this host.
  2. Try nc host port (note - not a ':', but a space here), if connection is refused, nc will terminate instantly. If not, you've established connection (try to type 'GET / HTTP/1.1' and Enter twice in this case).
  3. Just print output of socket.gethostname() and port variable in your code.

I suspect that in your case socket.gethostname() has been resolved into something different than localhost.

like image 155
George Shuklin Avatar answered Nov 23 '22 10:11

George Shuklin