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)
Connection refused
is generated by the kernel because receiving side refuses to establish TCP connection. Possible reasons are:
-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?
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).socket.gethostname()
and port
variable in your code.I suspect that in your case socket.gethostname()
has been resolved into something different than localhost
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With