1

When I run Twitter API code. I get this error:

tweepy.error.TweepError: Failed to send request: ('Connection aborted.', error(54, 'Connection reset by peer'))

Is this a problem on my end, and is there a way to avoid it, or tell the code to wait until the issue is fixe/try again instead of simply aborting?

NorthCat
  • 9,643
  • 16
  • 47
  • 50
auto
  • 1,062
  • 2
  • 17
  • 41
  • Without looking at your code there's no way to see where the problem is. I'm assuming you're using a Mac, correct? – Leb Jun 13 '15 at 12:33
  • yes it's a mac. It would help to know the source of the issue: the mac, the python code, Tweepy or Twitter. I would have to paste hundreds of lines of code here? – auto Jun 14 '15 at 19:22
  • I ran tweepy not more than an hour ago and it worked fine, yesterday too. That error is indicating a connection failure to the datastream. If the code is too long then paste the full error that you get from python. Without seeing that or the actual code I have to say it might be your Mac, but I'm not positive. I'm assuming it's the Mac because that error is more recurrent with them than other OS. – Leb Jun 14 '15 at 19:38
  • The problem isn't just when running Tweepy, it happens when I run the code for a long period of time. The code will work fine for a few hours, then all of a sudden I get this error. This only happens when working with large amounts of Twitter data which, due to their rate limits, takes hours and hours. It happens at different times throughout the process, however--sometimes an hour or 2 in, sometimes 3 or 4. – auto Jun 14 '15 at 19:58

2 Answers2

3

Try adjust your code as such to catch the error from twitter to determine if that's actually where it's coming from.

class listener(StreamListener):

    def on_data(self, status):
        try:
            # enter your code here

        except BaseException as e:
            print('failed on_status,',str(e)) # print the error code obtained from twitter
            time.sleep(5) # provide a time before resuming the code when an error arises

    def on_error(self, status):
        print(status)

Change the 5 in time.sleep() to however long you want it to.

Here is a list of the codes that twitter might through Error Codes & Responses

Leb
  • 15,483
  • 10
  • 56
  • 75
  • Thank you. I will try this. But how would I get it to resume where it left off, after sleeping? Won't the script just stop? – auto Jun 14 '15 at 23:45
  • No it does not terminate the script. `time.sleep(secs)¶ Suspend execution of the current thread for the given number of seconds.` From python docs. However, depending on the request and error you receive, twitter might disconnect you, but the script itself won't end. – Leb Jun 15 '15 at 01:32
  • I get: NameError: name 'StreamListener' is not defined – auto Jun 28 '15 at 05:46
  • `StreamListener` is a class defined in tweepy. You must have the import different, try `tweepy.StreamListener`. – Leb Jun 28 '15 at 13:20
  • Thanks. that worked for the error, now however, after running the code, nothing happens. I added print statements and looks like it doesn't run def on_data(self, status) – auto Jun 28 '15 at 22:07
  • Do `print(status)` after `try:`, what do you get? – Leb Jun 28 '15 at 22:13
  • Nothing happens. I have print statements throughout (after each indents), the only ones that work are right after/within the class statement (but not inside try, def or except), and outside/after the class statement. – auto Jun 28 '15 at 23:50
  • Post your whole code then or we can try work it out in chat. – Leb Jun 29 '15 at 00:08
-1

I had a similar problem, I found the error comes from an internet connection, that's why it was working before and suddenly stopped. You can see the error says that " Failed to send request" . so after I reconnected to the internet it worked fine.

mnzpython
  • 19
  • 1
  • 4