Question

I want to be able to leave my twython application running for long periods with no user interaction. It simply prints out tweets directed at me. The program is working but after a while I get the following error :

HTTPSConnectionPool(host='api.twitter.com', port=443): Max retries exceeded with url: /1.1/statuses/mentions_timeline.json?count=20&since_id=392227122070056960 (Caused by <class 'socket.error'>: [Errno 104] Connection reset by peer)

I am using the REST API, not streaming. The twython object is created with :

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

I then retrieve mentions with :

mentions = twitter.get_mentions_timeline(count=20,since_id=sinceID)

The code re-checks every 10 minutes or so, keeping track of the latest since_id, and so only prints out new tweets.

Questions:

1) Is the error a twython error or a python networking library error?

2) In the event of this error, what is the correct way to re-establish the connection with the twitter server without re-starting the program? Do I need to destroy the twython object first and then re-create it and so re-authenticate with the server? In the twython documentation I can only find mention of closing a connection when using the streaming API.

Any advice greatly appreciated.

Was it helpful?

Solution

1) Twitter throttles API requests. The REST API has a much more restrictive rate limit than the Streaming API. Here is a very detailed list of the limits for each call: https://developer.twitter.com/en/docs/basics/rate-limiting

2) You don't need to reestablish the connection. All you have to do is wait until you're beyond the throttling time limit (check link above). You could either space out the requests so that you don't hit the limit or you could store data locally in a cache to retrieve the last valid response.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top