can't understand [Errno 111] Connection refused
13,084
Connection refused
is generated by the kernel because receiving side refuses to establish TCP connection. Possible reasons are:
- No on listen on this host/port
- 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?
- Run you service. Without connecting to it run 'netstat -lnpt', it should provide you with list of services listening on this host.
- 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). - Just print output of
socket.gethostname()
andport
variable in your code.
I suspect that in your case socket.gethostname()
has been resolved into something different than localhost
.
Comments
-
CIsForCookies almost 2 years
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)
-
CIsForCookies over 5 yearsThanks! I'll check this
-
CIsForCookies over 5 years3 was the issue... I was running on a virtual machine and the hostname was indeed != "localhost". I hard-coded it as "localhost" and that fixed it
-
craq almost 5 years@CIsForCookies I think I have the same issue. How did you solve it? By editing
/etc/hosts
or changing a python setting somewhere? -
CIsForCookies almost 5 yearsI don't remember, but it was probably python. Though I would have tried it differently now (fixing the config is preferable to hard coding)