Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8162951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:54:12+00:00 2026-06-06T18:54:12+00:00

I am using Python and Requests to request data from Twitter’s streaming API .

  • 0

I am using Python and Requests to request data from Twitter’s streaming API. I would like to request data with a set of parameters, and after a period of time, change the request with a new set of parameters.

In the following simple, working example, I ask the Twitter streaming API for tweets with the keyword ‘python.’ After an hour, I ask the API for the keyword ‘ruby.’ However, to do this, I am creating a new requests object; I am not changing the original object.

import requests
import json
import time

USER = 'user'
PW = 'pw'

def make_request(keyword):
    r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
    data={'track': keyword}, auth=(USER,PW))

    for line in r.iter_lines():
        if line:
            print json.loads(line)

        if time.time() > start_of_last_request + 3600:
            break

count = 0

keywords = ['python', 'ruby']

while count < 2:
    start_of_last_request = time.time()

    make_request(keywords[count])

    count = count + 1

Ultimately, I will need to create a new query every hour and I am worried that I will be creating too many connections.

My questions are: Is there a better way to change the request to the Twitter API? Am I actually creating multiple connections? If so, how do I avoid this? (There’s a suggestion on how to close the previous connections, but the solution doesn’t quite make sense to me.)

I would appreciate any help. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:54:13+00:00Added an answer on June 6, 2026 at 6:54 pm

    As you linked, in order to close the connection after you are done with the request, set the keep-alive value to False instead of the default True:

    r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
                   data={'track': keyword}, auth=(USER,PW),
                   config={'keep_alive':False})
    

    This will make sure that the connection is closed once you’re done with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an isolated python script that simply captures data from Twitter's streaming API
I'm using the python module requests to get data from some API's and they
I'm trying to read data from Google Fusion Tables API into Python using the
Using Python I would like to find the date object for last Wednesday. I
I am using Apache/2.2.8 (Ubuntu) mod_python/3.3.1 Python/2.5.2 and I would like to preload the
Hey, I'm having some trouble using the QuickBase API from Python. From the QuickBase
I am using python's beautiful stone soup to extract data from this web page
Hey friends I am generating XML data using Python libraries as follow def multiwan_info_save(request):
How can I make a keep alive HTTP request using Python's urllib2?
I am using Python's xmlrpclib to make requests to an xml-rpc service. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.