Forums

OSError: [Errno 101] Network is unreachable

Hey there. I'm using the https://ultramsg.com/ api which is a WhatsApp API. Using Pythonanywhere's IP as a webhook so I can use the API publicly. But unfortunately, it says that the network is unreachable. I'm hoping you would whitelist the API i.e. "api.ultramsg.com"

api.ultramsg.com is already on the whitelist. If you want to use webhooks, you have to set up a web app.

Hi, I get the same error, trying to send a Whatsapp Message using http.client.HTTPSConnection("api.ultramsg.com") from my webapp. On my local machine it works just fine.

We need more details to help you.

What did you do? What did you expect? What happened instead?

I uploaded my flask app, that loads a Whatsapp Class to send a message. On Pythonanywhere I got an [Errno 101] whereas on my local machine the app runs fine. The app should have send my group a Whatsapp message with a pdf file attached. To identify the source of the error, I wrote this stand-alone script and got the same error:

19:43 ~/mysite $ cat try.py import http.client import base64 import urllib.parse import ssl import requests

ssl._create_default_https_context = ssl._create_unverified_context conn = http.client.HTTPSConnection("api.ultramsg.com") with open("repair_ticket.pdf", "rb") as image_file: encoded_string = base64.b64encode(image_file.read())

img_bas64=urllib.parse.quote_plus(encoded_string) payload = "token=token&to=number@g.us&document="+ img_bas64 + "&filename=hiplus.pdf" headers = { 'content-type': "application/x-www-form-urlencoded" } conn.request("POST", "/instance/messages/document", payload, headers) res = conn.getresponse() data = res.read() print(data.decode("utf-8"))

19:44 ~/mysite $ python3 try.py Traceback (most recent call last): File "/home/easybusiness/mysite/try.py", line 15, in <module> conn.request("POST", "/instance/messages/document", payload, headers) 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) OSError: [Errno 101] Network is unreachable 19:44 ~/mysite $

Make sure that your code is using proxy. See https://help.pythonanywhere.com/pages/403ForbiddenError#proxy-details