user3359257
user3359257

Reputation: 1

Python requests.exceptions.ConnectionError

I have the python script which crashes after the second alarm would be triggered. So when the first fault occurs the phone call happens. But when the second call would be triggerd the script crashes. The script will wait for the GPIO to be high and then will make a phone call to alert that there is a problem with the machine.

#!/usr/bin/env python3
#coding: utf8

import os
from os.path import join, dirname
from pprint import pprint
import vonage
import RPi.GPIO as GPIO
import time, datetime


client = vonage.Client(
    application_id="XXXXXXX",
    private_key="XXXXXX",

voice = vonage.Voice(client)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

################################### MAIN ##################################
  
  def call1():
    response = voice.create_call({

        'to': [{'type': 'phone', 'number': "XXXXX"}],
        'from': {'type': 'phone', 'number': "XXXXX"},
        'ncco': [{'action': 'talk','text': 'Störung Heizomat erkannt!',"language": "de-DE","style": 6 },]})

while True:
    if GPIO.input(18) == GPIO.HIGH:
        time.sleep (15)
        if GPIO.input(18) == GPIO.HIGH:
            make_call = call1()
            while GPIO.input(18) == GPIO.HIGH:
                time.sleep(60)
    time.sleep (5)

Error message:

Traceback (most recent call last):   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)   File "<string>", line 3, in raise_from   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()   File "/usr/lib/python3.9/http/client.py", line 1347, in getresponse
    response.begin()   File "/usr/lib/python3.9/http/client.py", line 307, in begin
    version, status, reason = self._read_status()   File "/usr/lib/python3.9/http/client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")   File "/usr/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)   File "/usr/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)   File "/usr/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer) ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(   File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 532, in increment
    raise six.reraise(type(error), error, _stacktrace)   File "/usr/lib/python3/dist-packages/six.py", line 718, in reraise
    raise value.with_traceback(tb)   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)   File "<string>", line 3, in raise_from   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()   File "/usr/lib/python3.9/http/client.py", line 1347, in getresponse
    response.begin()   File "/usr/lib/python3.9/http/client.py", line 307, in begin
    version, status, reason = self._read_status()   File "/usr/lib/python3.9/http/client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")   File "/usr/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)   File "/usr/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)   File "/usr/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer) urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File "/home/pi/Documents/Heizomat_Stoerung/call.py", line 80, in <module>
    make_call = call1()   File "/home/pi/Documents/Heizomat_Stoerung/call.py", line 50, in call1
    response = voice.create_call({   File "/home/pi/.local/lib/python3.9/site-packages/vonage/voice.py", line 22, in create_call
    return self._jwt_signed_post("/v1/calls", params or kwargs)   File "/home/pi/.local/lib/python3.9/site-packages/vonage/voice.py", line 85, in _jwt_signed_post
    self._client.api_host(), self._client.session.post(uri, json=params, headers=self._client._headers())   File "/usr/lib/python3/dist-packages/requests/sessions.py", line 590, in post
    return self.request('POST', url, data=data, json=json, **kwargs)   File "/usr/lib/python3/dist-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)   File "/usr/lib/python3/dist-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)   File "/usr/lib/python3/dist-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

Do you have any idea, why this happens?

Thanks in advance! Philipp

Upvotes: 0

Views: 1315

Answers (1)

Kejax
Kejax

Reputation: 416

If you just google what „Connection reset by peer“ you what happened. It‘s very simple, your connection was closed by the server you‘re connecting to. You‘re waiting too long for the server, so it‘s closing your connection because you timed out

I didn‘t watch the vonage API docs for long, but you should think of creating the client instance in the if where you do want to create a call.

#!/usr/bin/env python3
#coding: utf8

import os
from os.path import join, dirname
from pprint import pprint
import vonage
import RPi.GPIO as GPIO
import time, datetime

GPIO.setmode(GPIO.BOARD)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

################################### MAIN ##################################
def call1():
    response = voice.create_call({

        'to': [{'type': 'phone', 'number': "XXXXX"}],
        'from': {'type': 'phone', 'number': "XXXXX"},
        'ncco': [{'action': 'talk','text': 'Störung Heizomat erkannt!',"language": "de-DE","style": 6 },]})

while True:
    if GPIO.input(18) == GPIO.HIGH:
        time.sleep (15)
        if GPIO.input(18) == GPIO.HIGH:
            client = vonage.Client(
                application_id="XXXXXXX",
                private_key="XXXXXX",
            )

            voice = vonage.Voice(client)
            make_call = call1()
            while GPIO.input(18) == GPIO.HIGH:
                time.sleep(60)
    time.sleep (5)

If I didn‘t do a typo that should work for you

Upvotes: 1

Related Questions