Python Forum
maybe you could tell me why my script stops
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
maybe you could tell me why my script stops
#1
i have this script on raspbian
and  sometimes it fails and stops
could you help me to understand what's the problem?

here is the log of the script:

Error:
Traceback (most recent call last):   File "Documents/PY/cancelloaperto.py", line 46, in <module>     client.loop_forever()   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1378, in loop_forever     rc = self.loop(timeout, max_packets)   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 897, in loop     rc = self.loop_read(max_packets)   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1177, in loop_read     rc = self._packet_read()   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1766, in _packet_read     rc = self._packet_handle()   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2239, in _packet_handle     return self._handle_publish()   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2414, in _handle_publish     self._handle_on_message(message)   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2574, in _handle_on_message     self.on_message(self, self._userdata, message)   File "Documents/PY/cancelloaperto.py", line 37, in on_message     inviamail("Cancello Aperto", "[email protected]", "Appena aperto il cancello\nalle ore " + tempo)   File "Documents/PY/cancelloaperto.py", line 17, in inviamail     server = smtplib.SMTP('smtp.gmail.com:587')   File "/usr/lib/python3.4/smtplib.py", line 242, in __init__     (code, msg) = self.connect(host, port)   File "/usr/lib/python3.4/smtplib.py", line 321, in connect     self.sock = self._get_socket(host, port, self.timeout)   File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket     self.source_address)   File "/usr/lib/python3.4/socket.py", line 491, in create_connection     for res in getaddrinfo(host, port, 0, SOCK_STREAM):   File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo     for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno -2] Name or service not known
here's my code (i changed email details)

#!/usr/bin/env python3
import os
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
import time
import smtplib
import requests
from datetime import datetime

def inviamail(sogg, dest, testo):
        fromaddr = '[email protected]'
        toaddrs  = dest
        msg = testo
        sub = sogg
        username = '[email protected]'
        password = 'xxxxxx'
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login(username,password)
        message = 'Subject: {}\n\n{}'.format(sub, msg)
        server.sendmail(fromaddr, toaddrs, message)
        server.quit()
        return

while True:
        def on_connect(client, userdata, flags, rc):
                #print("Connected with result code "+str(rc))
                client.subscribe("mytopic")

        def on_message(client, userdata, msg):
                messaggio = str(msg.payload)
                if "APERTO" in messaggio:
                        t = datetime.now().time()
                        tempo = str(t)[0:8]
                        print(tempo+" - ACCESSO")
                        inviamail("Cancello Aperto", "[email protected]", "Appena aperto il cancello\$
                        client.disconnect()

        client = mqtt.Client()
        client.on_connect = on_connect
        client.on_message = on_message

        client.connect("127.0.0.1",1883,60)

        client.loop_forever()
Reply
#2
In line 37

inviamail("Cancello Aperto", "[email protected]", "Appena aperto il cancello\$
you seem to be missing a closing quote and parenthesis.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
That line was truncated (probably when copying), in the error message its complete.

I think that problem migh be in
       server = smtplib.SMTP('smtp.gmail.com:587')
try to replace it with
       server = smtplib.SMTP('smtp.gmail.com', 587)
Reply
#4
i m so sorry guys... i made a copy from linux terminal and copied bad... i update the post

this is actually the code

#!/usr/bin/env python3
import os
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
import time
import smtplib
import requests
from datetime import datetime

def inviamail(sogg, dest, testo):
fromaddr = '[email protected]'
toaddrs  = dest
msg = testo
sub = sogg
username = '[email protected]'
password = 'xxxxx'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
message = 'Subject: {}\n\n{}'.format(sub, msg)
server.sendmail(fromaddr, toaddrs, message)
server.quit()
return

while True:
def on_connect(client, userdata, flags, rc):
#print("Connected with result code "+str(rc))
client.subscribe("cancello")

def on_message(client, userdata, msg):
messaggio = str(msg.payload)
if "APERTO" in messaggio:
t = datetime.now().time()
tempo = str(t)[0:8]
print(tempo+" - ACCESSO")
inviamail("Cancello Aperto", "[email protected]", "Appena aperto il cancello\nalle ore " + tempo)
client.disconnect()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("127.0.0.1",1883,60)

client.loop_forever()
the code works fine
but once in a day or 2 it breaks with the error i wrote

thanks

(Apr-11-2017, 01:10 PM)zivoni Wrote: try to replace it with
       server = smtplib.SMTP('smtp.gmail.com', 587)
i've changed this line.. the script still works... let's see if it gonna break again.thanks

bytheway: if i want to run the script in background at startup in my raspberry how do you suggest me to do that?
shall i write down some error management in the script?
Reply
#5
Add & at the end of the command. Separated  with space
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
here's the log

Error:
Traceback (most recent call last):   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connection.py", line 141, in _new_conn     (self.host, self.port), self.timeout, **extra_kw)   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/util/connection.py", line 60, in create_connection     for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):   File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo     for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno -2] Name or service not known During handling of the above exception, another exception occurred: Traceback (most recent call last):   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 600, in urlopen     chunked=chunked)   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 345, in _make_request     self._validate_conn(conn)   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 844, in _validate_conn     conn.connect()   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connection.py", line 284, in connect     conn = self._new_conn()   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connection.py", line 150, in _new_conn     self, "Failed to establish a new connection: %s" % e) requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0xb6324590>: Failed to establish a new connection: [Errno -2] Name or service not known During handling of the above exception, another exception occurred: Traceback (most recent call last):   File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 423, in send     timeout=timeout   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 649, in urlopen     _stacktrace=sys.exc_info()[2])   File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/util/retry.py", line 376, in increment     raise MaxRetryError(_pool, url, error or ResponseError(cause)) requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.thingspeak.com', port=443): Max retries exceeded with url: /apps/thinghttp/send_request?api_key=HNB5SQVTCB6IT5N6 (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0xb6324590>: Failed to establish a new connection: [Errno -2] Name or service not known',)) During handling of the above exception, another exception occurred: Traceback (most recent call last):   File "/home/pi/Documents/PY/citofono.py", line 51, in <module>     client.loop_forever()   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1378, in loop_forever     rc = self.loop(timeout, max_packets)   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 897, in loop     rc = self.loop_read(max_packets)   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1177, in loop_read     rc = self._packet_read()   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1766, in _packet_read     rc = self._packet_handle()   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2239, in _packet_handle     return self._handle_publish()   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2414, in _handle_publish     self._handle_on_message(message)   File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2574, in _handle_on_message     self.on_message(self, self._userdata, message)   File "/home/pi/Documents/PY/citofono.py", line 18, in on_message     m = requests.get(url)   File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 70, in get     return request('get', url, params=params, **kwargs)   File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 56, in request     return session.request(method=method, url=url, **kwargs)   File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 488, in request     resp = self.send(prep, **send_kwargs)   File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 609, in send     r = adapter.send(request, **kwargs)   File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 487, in send     raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.thingspeak.com', port=443): Max retries exceeded with url: /apps/thinghttp/send_request?api_key=HNB5SQVTCB6IT5N6 (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0xb6324590>: Failed to establish a new connection: [Errno -2] Name or service not known',))
Reply
#7
well thanks guys i added an exception handling and works quite better
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pip stops waiting for python walker 6 1,212 Nov-28-2023, 06:55 PM
Last Post: walker
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 1,006 Nov-16-2022, 07:58 PM
Last Post: Winfried
  Know when the pyttsx3 engine stops talking UsualCoder 3 3,373 Aug-29-2021, 11:08 PM
Last Post: snippsat
  IDLE stops responding upon saving tompi1 2 2,008 Oct-01-2020, 05:44 PM
Last Post: Larz60+
  Python timer script stops before should ozstar 3 2,291 May-04-2020, 12:55 AM
Last Post: ozstar
  Python stops without errors shahgourav 4 2,863 Feb-04-2020, 11:44 PM
Last Post: micseydel
  First for loop stops after first iteration Divanova94 10 9,155 May-01-2019, 04:27 PM
Last Post: buran
  Python Thread stops execution neethuvp 1 3,472 Feb-18-2019, 06:36 PM
Last Post: micseydel
  randint stops changing values in a loop Naito 4 3,443 Jan-30-2019, 08:15 AM
Last Post: perfringo
  detect when scanner stops darter 1 2,468 Jun-29-2018, 10:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020