mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-23 06:50:29 +01:00
Merge pull request #90 from rahiel/master
raise exception if telegram times out on long-polling
This commit is contained in:
commit
3928e27cf4
1 changed files with 11 additions and 1 deletions
|
@ -20,6 +20,7 @@
|
||||||
"""This module contains methods to make POST and GET requests"""
|
"""This module contains methods to make POST and GET requests"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
from ssl import SSLError
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
@ -78,6 +79,11 @@ def post(url,
|
||||||
Returns:
|
Returns:
|
||||||
A JSON object.
|
A JSON object.
|
||||||
"""
|
"""
|
||||||
|
if "timeout" in data:
|
||||||
|
timeout = data["timeout"] + 1.
|
||||||
|
else:
|
||||||
|
timeout = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if InputFile.is_inputfile(data):
|
if InputFile.is_inputfile(data):
|
||||||
data = InputFile(data)
|
data = InputFile(data)
|
||||||
|
@ -90,7 +96,7 @@ def post(url,
|
||||||
data=data.encode(),
|
data=data.encode(),
|
||||||
headers={'Content-Type': 'application/json'})
|
headers={'Content-Type': 'application/json'})
|
||||||
|
|
||||||
result = urlopen(request).read()
|
result = urlopen(request, timeout=timeout).read()
|
||||||
except HTTPError as error:
|
except HTTPError as error:
|
||||||
if error.getcode() == 403:
|
if error.getcode() == 403:
|
||||||
raise TelegramError('Unauthorized')
|
raise TelegramError('Unauthorized')
|
||||||
|
@ -99,7 +105,11 @@ def post(url,
|
||||||
|
|
||||||
message = _parse(error.read())
|
message = _parse(error.read())
|
||||||
raise TelegramError(message)
|
raise TelegramError(message)
|
||||||
|
except SSLError as error:
|
||||||
|
if "The read operation timed out" == error.message:
|
||||||
|
raise TelegramError("Timed out")
|
||||||
|
|
||||||
|
raise TelegramError(error.message)
|
||||||
return _parse(result)
|
return _parse(result)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue