Forums

ConnectionRefusedError: [Errno 111] Connection refused with Flask and deta

When using pythonanywhere's web hosting, I was trying to access a deta base and deta drive, but it gave me the connection error.

Could this be due to deta's recent transition to deta.space?

You need to share more details about what you do,

My bad, I am trying to access a deta base with the line. The base name is notes and I initialized it with Deta.base("notes") and I am sure I have the correct API Key.

return render_template("index.html", notes=notes.fetch().items)

And it is throwing that error.

Free accounts cannot connect out from PythonAnywhere to random IP addresses. Free accounts can only connect to sites on our allow list. See https://help.pythonanywhere.com/pages/RequestingAllowlistAdditions/ You may also need to configure the library that you're using to use the proxy. Check the documentation of the library to find out how to do that and the details that you need are here

I replaced a few lines at where it is sending the request, it is now:

if not self.keep_alive or reinitializeConnection:
    self.client = http.client.HTTPConnection("proxy.server", port=3128, timeout=self.timeout)
    self.client.set_tunnel(host=self.host)

    self.client.request(
           method,
           url,
           headers=headers,
           body=body,
      )
      res = self.client.getresponse()
      return res

But I'm still getting the same error. Am I doing something wrong or does this just not work?

Have you reloaded your web app from the web app configuration page since you made that change?

Yes I have, it still does not work

What is the error that you are currently getting?

Here is my error:

http.client.CannotSendRequest: Request-sent
2023-03-03 01:04:08,865: Exception on / [GET]
Traceback (most recent call last):
File "/home/anonymousey/.virtualenvs/venv/lib/python3.10/site-packages/flask/app.py", line 2528, in wsgi_app
    response = self.full_dispatch_request()
File "/home/anonymousey/.virtualenvs/venv/lib/python3.10/site-packages/flask/app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
File "/home/anonymousey/.virtualenvs/venv/lib/python3.10/site-packages/flask/app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
File "/home/anonymousey/.virtualenvs/venv/lib/python3.10/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/home/anonymousey/nanotebook/helpers.py", line 36, in decorated_function
    return f(*args, **kwargs)
File "/home/anonymousey/nanotebook/main.py", line 42, in index
    return render_template("index.html", notes=notes.fetch().items)
File "/home/anonymousey/.virtualenvs/venv/lib/python3.10/site-packages/deta/base.py", line 203, in fetch
    _, res = self._fetch(query, limit, last)
File "/home/anonymousey/.virtualenvs/venv/lib/python3.10/site-packages/deta/base.py", line 189, in _fetch
    code, res = self._request("/query", "POST", payload, content_type=JSON_MIME)
File "/home/anonymousey/.virtualenvs/venv/lib/python3.10/site-packages/deta/service.py", line 74, in _request
    res = self._send_request_with_retry(method, url, headers, body)
File "/home/anonymousey/.virtualenvs/venv/lib/python3.10/site-packages/deta/service.py", line 115, in _send_request_with_retry
    self.client.request(
File "/usr/local/lib/python3.10/http/client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/lib/python3.10/http/client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.10/http/client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.10/http/client.py", line 1037, in _send_output
    self.send(msg)
File "/usr/local/lib/python3.10/http/client.py", line 975, in send
    self.connect()
File "/usr/local/lib/python3.10/http/client.py", line 1447, in connect
    super().connect()
File "/usr/local/lib/python3.10/http/client.py", line 941, in connect
    self.sock = self._create_connection(
File "/usr/local/lib/python3.10/socket.py", line 845, in create_connection
    raise err
File "/usr/local/lib/python3.10/socket.py", line 833, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

That definitely looks like it's not using the proxy. Is there some way to configure it to use requests rather than the base HTTP library?

I could but then I have to use requests to replace the places where I used the Deta library. I would rather avoid that but it seems unavoidable... is there any other solution?

It's either that or upgrade to a paid account.