Problems using PRAW (searching and posting) - python

I am trying to write a small program with PRAW the python library for Reddit API, and I am searching and posting to reddit greek content.
For example I have the following function:
def find_if_posted(usr, title, url):
for content in usr.search(title):
return True
for content in usr.search(url):
return True
return False
which tries to search if there is a post with the same title or url. usr is the reddit PRAW object. When it starts to search with the url I am getting the error:
Traceback (most recent call last):
File "C:\Users\hargikas\Dropbox\Private\src\reddit\auto_poster\leftist.py", line 133, in <module>
while (key is None) or find_if_posted(usr, key, content[key]):
File "C:\Users\hargikas\Dropbox\Private\src\reddit\auto_poster\leftist.py", line 86, in find_if_posted
for content in usr.search(url):
File "C:\Python34\lib\site-packages\praw\__init__.py", line 1108, in search
**kwargs):
File "C:\Python34\lib\site-packages\praw\__init__.py", line 515, in get_content
root = page_data.get(root_field, page_data)
AttributeError: 'str' object has no attribute 'get'
Also when I am trying to post something (using the submit function) I get this error:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 372, in _make_request
httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 374, in _make_request
httplib_response = conn.getresponse()
File "C:\Python34\lib\http\client.py", line 1162, in getresponse
raise ResponseNotReady(self.__state)
http.client.ResponseNotReady: Request-sent
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\requests\adapters.py", line 370, in send
timeout=timeout
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 597, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Python34\lib\site-packages\requests\packages\urllib3\util\retry.py", line 245, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\Python34\lib\site-packages\requests\packages\urllib3\packages\six.py", line 309, in reraise
raise value.with_traceback(tb)
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 374, in _make_request
httplib_response = conn.getresponse()
File "C:\Python34\lib\http\client.py", line 1162, in getresponse
raise ResponseNotReady(self.__state)
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', ResponseNotReady('Request-sent',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\hargikas\Dropbox\Private\src\reddit\auto_poster\leftist.py", line 140, in <module>
usr.submit('greekreddit' , key, url=content[key])
File "C:\Python34\lib\site-packages\praw\decorators.py", line 338, in wrapped
return function(cls, *args, **kwargs)
File "C:\Python34\lib\site-packages\praw\decorators.py", line 237, in wrapped
return function(obj, *args, **kwargs)
File "C:\Python34\lib\site-packages\praw\__init__.py", line 2218, in submit
retry_on_error=False)
File "C:\Python34\lib\site-packages\praw\decorators.py", line 163, in wrapped
return_value = function(reddit_session, *args, **kwargs)
File "C:\Python34\lib\site-packages\praw\__init__.py", line 561, in request_json
retry_on_error=retry_on_error)
File "C:\Python34\lib\site-packages\praw\__init__.py", line 402, in _request
response = handle_redirect()
File "C:\Python34\lib\site-packages\praw\__init__.py", line 375, in handle_redirect
timeout=timeout, **kwargs)
File "C:\Python34\lib\site-packages\praw\handlers.py", line 144, in wrapped
result = function(cls, **kwargs)
File "C:\Python34\lib\site-packages\praw\handlers.py", line 54, in wrapped
return function(cls, **kwargs)
File "C:\Python34\lib\site-packages\praw\handlers.py", line 99, in request
allow_redirects=False)
File "C:\Python34\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python34\lib\site-packages\requests\adapters.py", line 415, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ResponseNotReady('Request-sent',))
When I am searching using parameters of imgur.com or other "English" sites, I get no error. But when I use it over Greek sites, or posting Greek content I get the above errors.
Do you have an idea what I am doing wrong? I am a newbie with PRAW so any comment is appreciated. I am using Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 and PRAW Version: 2.1.21
Thank you in advance!
Harry

Related

Tornado: how to close file object if exception/error in data_received when streaming chunked data to file

I am running Tornado with stream_request_body and saving a file POST request to file on server. In prepare I create the file object and in post I close file object. How do I capture any error/exception in data_received so that I can close the file object properly?
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler, stream_request_body
#stream_request_body
class UploadHandler(RequestHandler):
def prepare(self):
self.file = open('uploaded_file', 'wb')
def data_received(self, chunk):
print(len(chunk))
self.file.write(chunk)
# force an exception
raise Exception('Error')
def post(self, *args, **kwargs):
self.file.close()
self.write('UPLOADED')
app = Application([(r'/upload', UploadHandler), ],
debug=True)
if __name__ == '__main__':
app.listen(7777)
IOLoop.current().start()
terminal output where tornado app is running
$ python upload.py
232
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/http1connection.py", line 659, in _read_fixed_body
ret = delegate.data_received(body)
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/routing.py", line 264, in data_received
return self.delegate.data_received(chunk)
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/web.py", line 2279, in data_received
return self.handler.data_received(data)
File "upload.py", line 13, in data_received
raise Exception('Error')
Exception: Error
terminal output with python interpreter for the request
>>> resp=requests.post('http://localhost:7777/upload', files={'file': open('test.txt', 'rb')})
Traceback (most recent call last):
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen
chunked=chunked,
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib64/python3.6/http/client.py", line 1346, in getresponse
response.begin()
File "/usr/lib64/python3.6/http/client.py", line 307, in begin
version, status, reason = self._read_status()
File "/usr/lib64/python3.6/http/client.py", line 276, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 756, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/util/retry.py", line 532, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/packages/six.py", line 734, in reraise
raise value.with_traceback(tb)
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen
chunked=chunked,
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib64/python3.6/http/client.py", line 1346, in getresponse
response.begin()
File "/usr/lib64/python3.6/http/client.py", line 307, in begin
version, status, reason = self._read_status()
File "/usr/lib64/python3.6/http/client.py", line 276, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
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 "<stdin>", line 1, in <module>
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/api.py", line 119, in post
return request('post', url, data=data, json=json, **kwargs)
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
RequestHandler has an on_connection_close method which is called when a connection is closed. You can override this to close the file object:
class UploadHandler(...):
...
def on_connection_close(self):
self.file.close()

104, Connection reset by peer. When sending multiple long http post requests

I have a kubernetes cluster (on GKE) which runs tornado.web.Application server, that calls heavy dask.distributed calculations on that cluster.
Now I want to test my server, and send it multiple requests at once, to see if it handles those requests as it should.
When I'm sending only one request, it does everything and behaves as expected, no matter how big the request is.
But when I send more than one request, after like 5 minutes (I also tried on server side just to sleep, without making any calculations and got the same results), I get the following:
Traceback (most recent call last):
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 706, in urlopen
chunked=chunked,
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 445, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 440, in _make_request
httplib_response = conn.getresponse()
File "/home/michael/anaconda3/lib/python3.7/http/client.py", line 1354, in getresponse
response.begin()
File "/home/michael/anaconda3/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/home/michael/anaconda3/lib/python3.7/http/client.py", line 267, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/home/michael/anaconda3/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/michael/anaconda3/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 756, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/util/retry.py", line 531, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/packages/six.py", line 734, in reraise
raise value.with_traceback(tb)
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 706, in urlopen
chunked=chunked,
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 445, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/home/michael/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py", line 440, in _make_request
httplib_response = conn.getresponse()
File "/home/michael/anaconda3/lib/python3.7/http/client.py", line 1354, in getresponse
response.begin()
File "/home/michael/anaconda3/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/home/michael/anaconda3/lib/python3.7/http/client.py", line 267, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/home/michael/anaconda3/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
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/michael/anaconda3/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/home/michael/anaconda3/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "<ipython-input-4-ce4ad22684d3>", line 24, in fire
response = requests.post(url, data=payload, headers=headers, timeout=3600)
File "/home/michael/anaconda3/lib/python3.7/site-packages/requests/api.py", line 119, in post
return request('post', url, data=data, json=json, **kwargs)
File "/home/michael/anaconda3/lib/python3.7/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/home/michael/anaconda3/lib/python3.7/site-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/home/michael/anaconda3/lib/python3.7/site-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/home/michael/anaconda3/lib/python3.7/site-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
And none of the requests returns.
How can I get the behaviour I need, without these errors?
I need a server that can handle many requests, and to test it from my computer.

SOLVED Python POST (how to "execute" an url?)

SOLVED
I will start off with I've never dealt with HTTP requests, and I've tried a few examples that I saw here on the website, but none of them worked for my case.
I want to "activate" scripts that are on a hardware device that I am trying to control. The device has its own HTTP server and by using various URLs I can change its variables, such url looks like this:
http://ipofdevice/ScriptName?varName=varValue&varName2=varValue2&varNameN=varValueN
I am trying to figure out how can I "activate" such URLs with python and I've gotten as far as, that I have to use post requests (I think?) and here is what I've tried so far:
I have already checked a few other posts and tried sample codes from there, but none of them worked, one such post was this:
How to send POST request?
Here is are two sample codes that I have tried, but did not work:
from urllib.parse import urlencode
from urllib.request import Request, urlopen
url = "http://127.0.0.1/ScriptName"
post_fields = { 'testVar' : 'hello', 'secondVar' : 'fromPython' }
request = Request(url, urlencode(post_fields).encode())
json = urlopen(request).read().decode()
And the json line threw an exception with a very long Traceback
Traceback (most recent call last):
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 289, in _read_status
status = int(status)
ValueError: invalid literal for int() with base 10: '400,'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 1348, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 1323, in do_open
r = h.getresponse()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1322, in getresponse
response.begin()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 303, in begin
version, status, reason = self._read_status()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 293, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: HTTP/1.0 400, Invalid Request
This is the code from my second attempt:
import requests
base_url = "http://127.0.0.1/ScriptName"
post_fields = { 'testVar' : 'hello', 'secondVar' : 'fromPython' }
response = requests.post(final_url, data=post_fields)
With an even much longer traceback, which I will not post simply because it adds too much clutter and I dont think its needed, since obviously I am doing something fundamentally wrong.
I dont need anything fancy, just to be able to run a link like the one in the beginning of the thread, I am not even sure if POST request is what I need.
In javascript I was able to do it like this (but javascript lacks other things that I need):
var wnd = window.open("http://127.0.0.1/ScriptName?var=val");
wnd.close();
e.preventDefault();
EDIT: Adding the traceback for the second python attempt:
Traceback (most recent call last):
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1322, in getresponse
response.begin()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 303, in begin
version, status, reason = self._read_status()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 293, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: HTTP/1.0 400, Invalid Request
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 400, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 734, in reraise
raise value.with_traceback(tb)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1322, in getresponse
response.begin()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 303, in begin
version, status, reason = self._read_status()
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 293, in _read_status
raise BadStatusLine(line)
urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine('HTTP/1.0 400, Invalid Request\r\n'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 119, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Darkbound\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('HTTP/1.0 400, Invalid Request\r\n'))
EDIT 2: Solved, turns out that I actually needed a GET request as #MrBean Bremen suggested , all I had to do was:
import requests
requests.get(myurl)
Try this
import requests
tesVar = hello
secondVar = fromPython
baseurl = "http://127.0.0.1/ScriptName?{'tesVar'}&{'secondVar'}"
response = requests.get(baseurl)
print(response)

Can't continue a program when binance api(connection) error occured

I'm fetching OHLCV data from Binance via Binance api on AWS-EC2.
But sometime(once a couple of days) error "104, 'ECONNRESET'" occur and program stop.
The program written in Python3.7.3 and run on AWS-EC2
The code is following. But this code couldn't continue.
I want to continue the program running when any api or connection error occured.
How should I handle this error ?
while True:
..............
try :
klines = client.get_historical_klines(ticker_symbol, Client.KLINE_INTERVAL_1MINUTE, _from_str, _until_str)
except BinanceAPIException as e:
print(e)
print('Something went wrong. Error occured at %s. Wait for 1 hour.' % (datetime.datetime.now().astimezone(timezone('UTC'))))
sleep(3600)
continue
..............
All error massege are following.
Traceback (most recent call last):
File
"/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py",
line 600, in urlopen
chunked=chunked) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py",
line 384, in _make_request
six.raise_from(e, None) File "", line 2, in raise_from File
"/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py",
line 380, in _make_request
httplib_response = conn.getresponse() File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py",
line 1321, in getresponse
response.begin() File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py",
line 296, in begin
version, status, reason = self._read_status() File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py",
line 257, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/socket.py", line
589, in readinto
return self._sock.recv_into(b) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py",
line 312, in recv_into
return self.recv_into(*args, **kwargs) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py",
line 302, in recv_into
raise SocketError(str(e)) OSError: (104, 'ECONNRESET')
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/adapters.py",
line 449, in send
timeout=timeout File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py",
line 638, in urlopen
_stacktrace=sys.exc_info()[2]) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/util/retry.py",
line 368, in increment
raise six.reraise(type(error), error, _stacktrace) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/packages/six.py",
line 685, in reraise
raise value.with_traceback(tb) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py",
line 600, in urlopen
chunked=chunked) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py",
line 384, in _make_request
six.raise_from(e, None) File "", line 2, in raise_from File
"/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/connectionpool.py",
line 380, in _make_request
httplib_response = conn.getresponse() File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py",
line 1321, in getresponse
response.begin() File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py",
line 296, in begin
version, status, reason = self._read_status() File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/http/client.py",
line 257, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/socket.py", line
589, in readinto
return self._sock.recv_into(b) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py",
line 312, in recv_into
return self.recv_into(*args, **kwargs) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py",
line 302, in recv_into
raise SocketError(str(e)) urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"BTC_USDT_BINANCE_minutes.py", line 54, in
klines = client.get_historical_klines(ticker_symbol, Client.KLINE_INTERVAL_1MINUTE, _from_str, _until_str) File
"/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/binance/client.py",
line 765, in get_historical_klines
first_valid_ts = self._get_earliest_valid_timestamp(symbol, interval) File
"/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/binance/client.py",
line 723, in _get_earliest_valid_timestamp
endTime=None File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/binance/client.py",
line 705, in get_klines
return self._get('klines', data=params) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/binance/client.py",
line 207, in _get
return self._request_api('get', path, signed, version, **kwargs) File
"/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/binance/client.py",
line 181, in _request_api
return self._request(method, uri, signed, **kwargs) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/binance/client.py",
line 175, in _request
response = getattr(self.session, method)(uri, **kwargs) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/sessions.py",
line 546, in get
return self.request('GET', url, **kwargs) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/sessions.py",
line 533, in request
resp = self.send(prep, **send_kwargs) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/sessions.py",
line 646, in send
r = adapter.send(request, **kwargs) File "/home/ec2-user/.pyenv/versions/3.7.3/lib/python3.7/site-packages/requests/adapters.py",
line 498, in send
raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.',
OSError("(104, 'ECONNRESET')"))
Assuming that you are using python-binance module, most likely the error happens because you are trying to reuse the client after long time (server timeout). You can try to recreate the client first:
...
client = Client(api_key, api_secret)
while True:
...
try :
klines = client.get_historical_klines(ticker_symbol, Client.KLINE_INTERVAL_1MINUTE, _from_str, _until_str)
except BinanceAPIException as e:
print(e)
print('Something went wrong. Error occured at %s. Wait for 1 hour.' % (datetime.datetime.now().astimezone(timezone('UTC'))))
sleep(3600)
client = Client(api_key, api_secret)
continue
...
The reason is because the [Client][2] uses requests Session underneath which imply usage of urllib3's connection pooling. This connection pooling can not be reused after timeout.

error handling for network disconnect error

I am using pyowm to get weather update after each minutes. If I disconnect my internet or low internet what error will be occure and how can i handle this error.For try..except method..what possible exception have to use...??
my code is....
import tkinter as tk
from tkinter import *
import pyowm
from PIL import ImageTk, Image
win=tk.Tk()
#frame_for_showing_weather.
weather_frame=Frame(win,bg='black',width='300',height='300')
weather_frame.place(x=0,y=0)
weath=Label(weather_frame,font=('Agency',100),fg='white',bg='black')
weath.place(x=10,y=10)
temp1=''
owm=pyowm.OWM('my api key')
def obs():
global temp1
observation = owm.weather_at_place('Barisal,BD')
w=observation.get_weather()
temp2=w.get_temperature('celsius')
if(temp2!=temp1):
temp1=temp2['temp']
weath.config(text=str(int(temp1))+chr(176))
print('Alhamdulillah,Yeah that is worked')
weath.after(60000,obs)
obs()
win.mainloop()
please someone explain...I am new at python.
if i drop my connection this big error occure...
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request
six.raise_from(e, None)
File "<string>", line 2, in raise_from
File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request
httplib_response = conn.getresponse()
File "C:\Program Files (x86)\Python35-32\lib\http\client.py", line 1197, in getresponse
response.begin()
File "C:\Program Files (x86)\Python35-32\lib\http\client.py", line 297, in begin
version, status, reason = self._read_status()
File "C:\Program Files (x86)\Python35-32\lib\http\client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "C:\Program Files (x86)\Python35-32\lib\socket.py", line 575, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\adapters.py", line 445, in send
timeout=timeout
File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\util\retry.py", line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\packages\six.py", line 686, in reraise
raise value
File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 306, in _raise_timeout
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='api.openweathermap.org', port=80): Read timed out. (read timeout=2)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\lib\site-packages\pyowm\commons\http_client.py", line 24, in get_json
timeout=self.timeout, verify=self.verify_ssl_certs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\adapters.py", line 526, in send
raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='api.openweathermap.org', port=80): Read timed out. (read timeout=2)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "C:\Program Files (x86)\Python35-32\lib\tkinter\__init__.py", line 596, in callit
func(*args)
File "C:\Users\James Bond\Desktop\test.py", line 23, in obs
observation = owm.weather_at_place('Barisal,BD')
File "C:\Program Files (x86)\Python35-32\lib\site-packages\pyowm\webapi25\owm25.py", line 210, in weather_at_place
_, json_data = self._wapi.cacheable_get_json(uri, params=params)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\pyowm\commons\http_client.py", line 44, in cacheable_get_json
status_code, data = self.get_json(uri, params=params, headers=headers)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\pyowm\commons\http_client.py", line 30, in get_json
raise api_call_error.APICallTimeoutError('API call timeouted')
pyowm.exceptions.api_call_error.APICallTimeoutError: Exception in calling OWM web API.
Reason: API call timeouted
Caused by: None
how can i handle this error...?

Categories