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 $