PlayBoyXXX
@PlayBoyXXX
Уже не школьник

Как сделать авто-перезагрузку кода Python?

Здравствуйте!
Дабы не задавать больше таких вопросов, поступаю в ВУЗ на программиста. Сделал простенькую программку для отправки скриншотов во время экзамена в телеграмм. Заметил краш exe-шника при обрыве соединения с интернетом.
Необходимо сделать так, чтобы при любых ошибках exe-шник перезапускался.

import pyautogui
import telebot
from pynput import keyboard

bot = telebot.TeleBot("TOKEN", parse_mode=None)
chat_id = 'CHAT_ID'


def sendphoto():
    photo = open('screenshot.png', 'rb')
    bot.send_photo(chat_id=chat_id, photo=photo)


def on_release(key):
    if key == keyboard.Key.insert:
        screen = pyautogui.screenshot('screenshot.png')
        print(screen)
        sendphoto()
    if key == keyboard.Key.ctrl_l:
        bot.send_message(chat_id=chat_id, text='текст')
    if key == keyboard.Key.alt_l:
        bot.send_message(chat_id=chat_id, text='текст')
    if key == keyboard.Key.ctrl_r:
        bot.send_message(chat_id=chat_id, text='текст')


with keyboard.Listener(on_release=on_release) as listener:
    listener.join()


Ошибка:
Traceback (most recent call last):
  File "urllib3\connectionpool.py", line 699, in urlopen
  File "urllib3\connectionpool.py", line 445, in _make_request
  File "<string>", line 3, in raise_from
  File "urllib3\connectionpool.py", line 440, in _make_request
  File "http\client.py", line 1374, in getresponse
  File "http\client.py", line 318, in begin
  File "http\client.py", line 287, in _read_status
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "requests\adapters.py", line 439, in send
  File "urllib3\connectionpool.py", line 755, in urlopen
  File "urllib3\util\retry.py", line 532, in increment
  File "urllib3\packages\six.py", line 769, in reraise
  File "urllib3\connectionpool.py", line 699, in urlopen
  File "urllib3\connectionpool.py", line 445, in _make_request
  File "<string>", line 3, in raise_from
  File "urllib3\connectionpool.py", line 440, in _make_request
  File "http\client.py", line 1374, in getresponse
  File "http\client.py", line 318, in begin
  File "http\client.py", line 287, in _read_status
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 28, in <module>
    listener.join()
  File "pynput\_util\__init__.py", line 276, in join
  File "six.py", line 718, in reraise
  File "pynput\_util\__init__.py", line 228, in inner
  File "pynput\keyboard\_win32.py", line 290, in _process
  File "pynput\_util\__init__.py", line 144, in inner
  File "main.py", line 18, in on_release
    sendphoto()
  File "main.py", line 11, in sendphoto
    bot.send_photo(chat_id=chat_id, photo=photo)
  File "telebot\__init__.py", line 1825, in send_photo
  File "telebot\apihelper.py", line 494, in send_photo
  File "telebot\apihelper.py", line 156, in _make_request
  File "requests\sessions.py", line 542, in request
  File "requests\sessions.py", line 655, in send
  File "requests\adapters.py", line 498, in send
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
  • Вопрос задан
  • 205 просмотров
Решения вопроса 1
Vindicar
@Vindicar
RTFM!
Быстрое и грязное решение
while True:
    try:
        ... # твой код
    except Exception as err:
        print(err)
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы