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 7804789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:02:26+00:00 2026-06-02T02:02:26+00:00

I am using the Python Requests module to consume the Twitter streaming API, here

  • 0

I am using the Python Requests module to consume the Twitter streaming API, here is my code:

self.conn = self.session.post(url, data=self.parameters, headers=self.headers)
    print >> sys.stderr, 'Connected to Twitter Streaming API'
    try:
        for line in self.conn.iter_content(self.ITER_BYTES):
            self.status += line
            if line.endswith(self.DIVIDER) and self.status.strip():
                self.handler.handle(self.status)
                self.status = ""
    except Exception as e:
        pass

When I end this script with a KeyboardInterrupt or by passing it a termination signal, I am getting the following stack error:

^CTraceback (most recent call last):
  File "python-test.py", line 18, in <module>
    api.start()
  File "bin/polygraph/api/twitter/streaming.py", line 95, in start
    self.conn = self.session.post(url, data=self.parameters, headers=self.headers)
  File "venv/lib/python2.7/site-packages/requests/sessions.py", line 258, in post
    return self.request('post', url, data=data, **kwargs)
  File "venv/lib/python2.7/site-packages/requests/sessions.py", line 208, in request
    r.send(prefetch=prefetch)
  File "venv/lib/python2.7/site-packages/requests/models.py", line 575, in send
    timeout=self.timeout,
  File "venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 383, in urlopen
    body=body, headers=headers)
  File "venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 261, in _make_request
    httplib_response = conn.getresponse()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1013, in getresponse
    response.begin()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 402, in begin
    version, status, reason = self._read_status()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 360, in _read_status
    line = self.fp.readline()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 430, in readline
    data = recv(1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 219, in recv
    return self.read(buflen)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 138, in read
    return self._sslobj.read(len)
KeyboardInterrupt

Is there any way to avoid this or gracefully exit from the connection?

  • 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-02T02:02:27+00:00Added an answer on June 2, 2026 at 2:02 am

    You simply need to catch the KeyboardInterrupt exception. This does not inherit from Exception, hence you don’t currently catch it.

    try:
       do_something()
    except KeyboardInterrupt:
       cleanup()
    

    This is another example of why except Exception is a bad idea. Not only will it catch things you didn’t intend it to, but it may not catch things you wanted it to. Catch explicit exceptions.

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

Sidebar

Related Questions

I am using Python's xmlrpclib to make requests to an xml-rpc service. Is there
Using Python module re, how to get the equivalent of the \w (which matches
I am using a python script to do my webserver using the BaseHTTPServer module.
I've been using the Freebase Python module successfully to read data, and today I
I'm not experienced in Python, and I need the requests module. I've tried installing
I am using a Python module ( PyCLIPS ) and Django 1.3. I want
My project makes various calls to external API's using Python's urllib2.urlopen . I'm using
How can I make a keep alive HTTP request using Python's urllib2?
Using Python I want to be able to draw text at different angles using
Using python 2.4 and the built-in ZipFile library, I cannot read very large zip

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.