使用Python Twitter流推文时,收到错误消息:OpenSSL.SSL.SysCallError:(104,'ECONNRESET')

时间:2018-12-07 19:44:12

标签: pyopenssl

我得到了这个错误,我不知道它来自哪里以及从哪里开始调试。我完全不了解此错误。我将在这里发布我的代码。我在代码中利用了多线程功能,以将推文定期保存在单独的文件中。我的程序可能会运行10天以上,然后向我抛出该错误消息。也许Twitter重置了我的连接?

import twitter
import json
import time

from tqdm import tqdm
import string
from datetime import timedelta, datetime

import threading
import os

consumer_key = '*'
consumer_secret = '*'
access_token_key = '*'
access_token_secret = '*'

api = twitter.Api(consumer_key = consumer_key,\
                  consumer_secret = consumer_secret,\
                  access_token_key = access_token_key,\
                  access_token_secret = access_token_secret)

os.chdir('*/social_media/streamed_tweets')

#multiThreading class and functions starts here
class timerThread(threading.Thread):
    def __init__(self, threadID, name, stoptime, stopevent): #remember to change args accordingly
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.stoptime = stoptime
        self.stopevent = stopevent
    def run(self):
        print("Starting " + self.name)
        twiTimer(self.stoptime, self.stopevent)
        print("Exiting " + self.name)

class streamerThread(threading.Thread):
    def __init__(self, threadID, name, keywords, stop): #remember to change args accordingly
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.keywords = keywords
        self.stop = stop
    def run(self):
        print("Starting " + self.name)
        twiStream(self.keywords, self.stop)
        print("Exiting " + self.name)

def twiTimer(stop_time, stop_event):
    while True:
        now1 = datetime.now()
        t = str(now1.timetz())
        if t[:8] == stop_time: #make it t[:8] for daily loop
            stop_event.set()
            print('stop triggered')
            time.sleep(1) #wait to pass the '00' time
        time.sleep(0.1)

def twiStream(keywords, stop_event):
    while True:
        stream = api.GetStreamFilter(track = keywords, languages=['en'], filter_level = None)
        now2 = datetime.now()
        filename = str(now2.date())+'.txt' #change this to date() for daily loop
        f = open(filename, 'w+')
        stop_event.clear() #reset the event
        print(now2)
        with tqdm() as pbar:
            while not stop_event.is_set():
                counter = 2
                for tweet in stream:
                    if counter <= 0:
                        break
                    f.write(json.dumps(tweet))
                    f.write('\n')
                    pbar.update()
                    counter -= 1
        f.close()
        time.sleep(0.5)
#multiThreading class and functions ends here

def main():
    keywords = ['*']

stop = threading.Event()
stop_at = '00:00:00' #make it '00:00:00' for daily loop

#count = 10

thread1 = timerThread(1, 'timerThread', stop_at, stop)
thread2 = streamerThread(2, 'streamerThread', keywords, stop)

thread1.start()
thread2.start()

print("Exiting Main Thread")

if __name__ == "__main__":
    main()

这是完整的错误消息:

Exception in thread streamerThread:

Traceback (most recent call last):

  File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 285, in recv_into

    return self.connection.recv_into(*args, **kwargs)

  File "/home/abe/anaconda3/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1814, in recv_into

    self._raise_ssl_error(self._ssl, result)

File "/home/abe/anaconda3/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1631, in _raise_ssl_error

  raise SysCallError(errno, errorcode.get(errno))

OpenSSL.SSL.SysCallError: (104, 'ECONNRESET')



During handling of the above exception, another exception occurred:



Traceback (most recent call last):

  File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 331, in _error_catcher

   yield

 File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 637, in read_chunked

  self._update_chunk_length()

 File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 569, in _update_chunk_length

   line = self._fp.fp.readline()

 File "/home/abe/anaconda3/lib/python3.6/socket.py", line 586, in readinto

   return self._sock.recv_into(b)

  File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 290, 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/abe/anaconda3/lib/python3.6/site-packages/requests/models.py", line 749, in generate

   for chunk in self.raw.stream(chunk_size, decode_content=True):

 File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 461, in stream

  for line in self.read_chunked(amt, decode_content=decode_content):

 File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 665, in read_chunked

  self._original_response.close()

 File "/home/abe/anaconda3/lib/python3.6/contextlib.py", line 99, in __exit__

 self.gen.throw(type, value, traceback)

  File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 349, in _error_catcher

   raise ProtocolError('Connection broken: %r' % e, e)

urllib3.exceptions.ProtocolError: ('Connection broken: OSError("(104, \'ECONNRESET\')",)', OSError("(104, 'ECONNRESET')",))


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

 File "/home/abe/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner

   self.run()

File "streamer.py", line 48, in run
twiStream(self.keywords, self.stop)

 File "streamer.py", line 72, in twiStream

  for tweet in stream:

  File "/home/abe/anaconda3/lib/python3.6/site-packages/twitter/api.py", line 4575, in GetStreamFilter

  for line in resp.iter_lines():

 File "/home/abe/anaconda3/lib/python3.6/site-packages/requests/models.py", line 793, in iter_lines

   for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):

 File "/home/abe/anaconda3/lib/python3.6/site-packages/requests/models.py", line 752, in generate

  raise ChunkedEncodingError(e)

requests.exceptions.ChunkedEncodingError: ('Connection broken: OSError("(104, \'ECONNRESET\')",)', OSError("(104, 'ECONNRESET')",))

0 个答案:

没有答案