Hello has anyone had this error when using pyTelegramBotAPI:
raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forced to be aborted by the remote host', None, 10054, None)).
This is the code I am using:
import telebot
import time
import logging
Token = TOKEN
bot = telebot.TeleBot(Token)
def send_signals(text):
#sendMessage
chats = chatID
try:
bot.send_message(chats, text)
except (ConnectionAbortedError, ConnectionResetError, ConnectionRefusedError, ConnectionError):
time.sleep(1)
bot.send_message(chats, text)
The bot has to send 3 messages, for example it sends a first message at 2:21 (it is not an absolute time), then another at 2:30 and when trying to send the last one sometimes I get this error. Or even from the first message it sends me the error.
Any idea how to solve it?
Related
while reworking Fastapi WebSockets tutorial received error on websocket connection lost, because of web page close or reload.
RuntimeError: Cannot call "receive" once a disconnect message has been received.
it was not critical, nor crash server. but leaving it be does no feels wright.
how to handle this WebSocket error?
-
A way to handle this error notification was to add try / except RuntimeError
while True:
try:
msg = await websocket.receive()
except RuntimeError:
break
Hi I have been struggling with this script for a while and the last line is starting to drive me crazy. I want to take a picture every minute and send it to telegram.
import telebot
import picamera
# Set token
token = "PLACE TOKEN HERE"
# Connect to our bot
bot = telebot.TeleBot(token)
# Sets the id for the active chat
chat_id=PLACE CHAT ID HERE
# Get the photo
camera=picamera.PiCamera()
camera.capture('./capture.jpg')
camera.close()
# Sends a message to the chat
bot.send_photo(chat_id, photo=open('./capture.jpg', 'rb'))
ERROR: TypeError: send_photo() got an unexpected keyword argument 'photo'
Help would be greatly appreciated.
I have improved the script. If it helps anyone here is the code im currently using:
import time
import requests
import telebot
import picamera
from time import sleep
# Set token
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Connect to our bot
bot = telebot.TeleBot(token)
# Sets the id for the active chat
chat_id="xxxxxxxxx"
# Repeat function
def repeat():
# Set camera
camera=picamera.PiCamera()
# Take picture
camera.capture('./capture.jpg')
# Camera close
camera.close()
# Send photo to telegram
bot.send_photo(chat_id=chat_id, photo=open('capture.jpg', 'rb'))
# Sleep 60 seconds
time.sleep(60)
while True:
repeat()
It works but sometimes the script will crash with one of these errors:
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
requests.exceptions.ConnectionError: ('Connection aborted.', timeout())
Any idea how to solve those errors?
I just wrote this:
try:
r = requests.get('http://example.com')
except requests.exceptions.ConnectionError as e:
print(e)
And I got this output:
('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
Does anyone know how could I get different types of connection errors? Like 'connection aborted', 'connection refused' and 'connection reset' from this exception and handle them?
If your goal is to get the response message and then handle them. You can try this code.
import requests
response = requests.get("http://www.example.com")
print(response.status_code)
print(response.reason)
I have a problem with my socket , it is well functioning but when i close the client / close the client window the server lost the connection ( the server needs to stay open and wait for other connection)
while True:
rlist, wlist, xlist = select.select([server_socket]+open_client_sockets, open_client_sockets, [])
for current_socket in rlist:
if current_socket is server_socket:
(new_socket, address) = server_socket.accept()
open_client_sockets.append(new_socket)
print 'new member : ' + str(address)
else:
data = current_socket.recv(1024)
print data
if data == "":
open_client_sockets.remove(current_socket)
print 'Connection with client closed.'
else:
send_messages(data)
The problem is in this part -
if data == "":
open_client_sockets.remove(current_socket)
print 'Connection with client closed.
This is the error -
data = current_socket.recv(1024)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
I didn't get this error in my previous socket
When a client does a graceful close of the socket such as client_socket.shutdown(socket.SHUT_WR) the server will receive all data and then its next recv call will get 0 bytes. You've coded for this case.
When the client exits without a graceful shutdown, the underlying socket implementation will do an ungraceful termination which includes sending a RESET to the server. In this case, the server gets the exception you've seen. It means that at the socket level there is no guarantee that the server received all of its data.
You should update your client to be graceful about closing and also decide what your policy should be on ungraceful exit.
I'd like to use the Requests package to connect to the streaming API of a web service. Suppose I use the following code to send a request, receive the response and iterate through the lines of response as they arrive:
import requests
r = requests.get('http://httpbin.org/stream/20', stream=True)
for line in r.iter_lines():
if line:
print line
While waiting to receive new data, we are basically waiting for r.iter_lines() to generate a new piece of data. But what if I lose internet connection while waiting? How can I find out so I can attempt to reconnect?
You can disconnect from your network to have a try. Requests raise such error:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /stream/20 (Caused by : [Errno -3] Temporary failure in name resolution)
The error message shows Requests already retries for network error. You can refer to this answer for setting the max_retries. If you wants more customization (e.g. waits between retries), do it in a loop:
import socket
import requests
import time
MAX_RETRIES = 2
WAIT_SECONDS = 5
for i in range(MAX_RETRIES):
try:
r = requests.get('http://releases.ubuntu.com/14.04.1/ubuntu-14.04.1-desktop-amd64.iso',
stream=True, timeout=10)
idx = 1
for chunk in r.iter_content(chunk_size=1024):
if chunk:
print 'Chunk %d received' % idx
idx += 1
break
except requests.exceptions.ConnectionError:
print 'build http connection failed'
except socket.timeout:
print 'download failed'
time.sleep(WAIT_SECONDS)
else:
print 'all tries failed'
EDIT: I tested with a large file. I used iter_content instead, because it's a binary file. iter_lines is based on iter_content (source codes), so I believe the behaviour is same. Procedure: run the codes with network connected. After receiving some chunks, disconnect. Wait 2-3 seconds, reconnect, the downloading continued. So requests package DOES retry for connection lost in the iteration.
Note: If no network when build the connection (requests.get()), ConnectionError is raised; if network lost in the iter_lines / iter_content, socket.timeout is raised.