Tweepy: Filtering stream gives multiple errors
Question: I'm trying to run the following script from this tutorial [tulane.edu-
twitter](http://www.tulane.edu/~howard/CompCultES/twitter.html) and I'm
getting the following errors:

    
    
      File "GetTextFromTwitter.py", line 61, in <module>
        stream.filter(track=['de'], languages=['es'])
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 428, in filter
        self._start(async)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 346, in _start
        self._run()
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 239, in _run
        verify=self.verify)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
        resp = self.send(prep, **send_kwargs)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
        r = adapter.send(request, **kwargs)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
        timeout=timeout
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
        body=body, headers=headers)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 349, in _make_request
        conn.request(method, url, **httplib_request_kw)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 995, in request
        self._send_request(method, url, body, headers)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 1029, in _send_request
        self.endheaders(body)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 991, in endheaders
        self._send_output(message_body)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 848, in _send_output
        self.send(message_body)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 820, in send
        self.sock.sendall(data)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 208, in sendall
        sent = self._send_until_done(data)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 198, in _send_until_done
        return self.connection.send(data)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/OpenSSL/SSL.py", line 947, in send
        raise TypeError("data must be a byte string")
    TypeError: data must be a byte string
    

I also tried to follow the tutorial on
[youtube](https://www.youtube.com/watch?v=pUUxmvvl2FE&spfreload=10) and I'm
getting the same errors:

    
    
     File "tweepyTest.py", line 39, in <module>
        twitterStream.filter(track=["car"])
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 428, in filter
        self._start(async)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 346, in _start
        self._run()
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 239, in _run
        verify=self.verify)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
        resp = self.send(prep, **send_kwargs)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
        r = adapter.send(request, **kwargs)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
        timeout=timeout
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
        body=body, headers=headers)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 349, in _make_request
        conn.request(method, url, **httplib_request_kw)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 995, in request
        self._send_request(method, url, body, headers)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 1029, in _send_request
        self.endheaders(body)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 991, in endheaders
        self._send_output(message_body)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 848, in _send_output
        self.send(message_body)
      File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 820, in send
        self.sock.sendall(data)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 208, in sendall
        sent = self._send_until_done(data)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 198, in _send_until_done
        return self.connection.send(data)
      File "/Users/enricok/anaconda/lib/python2.7/site-packages/OpenSSL/SSL.py", line 947, in send
        raise TypeError("data must be a byte string")
    TypeError: data must be a byte string
    

A couple of days ago it worked perfectly (still having the .csv it returned).
But now like with an other script I tried, it gives me multiple errors when
using the `.filter` method. If I replace `.filter()` with `.sample()` though,
it works. The only thing I can remember, was doing a `brew update` that might
have broken something. Here the code for the first mentioned tutorial that
tries to filter Spanish tweets. Any ideas how to fix this?

    
    
    import tweepy
    import requests
    
    from tweepy import API
    from tweepy import Stream
    from tweepy import OAuthHandler
    from tweepy.streaming import StreamListener
    
    API_KEY = ''
    API_SECRET = ''
    ACCESS_TOKEN = ''
    ACCESS_TOKEN_SECRET = ''
    
    key = tweepy.OAuthHandler(API_KEY, API_SECRET)
    key.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    
    api = tweepy.API(key)
    
    
    class Stream2Screen(tweepy.StreamListener):
        def __init__(self, api=None):
            self.api = api or API()
            self.n = 0
            self.m = 20
    
        def on_status(self, status):
            print status.text.encode('utf8')
            self.n = self.n+1
            if self.n < self.m:
                return True
            else:
                print 'tweets = '+str(self.n)
                return False
    
    stream = tweepy.streaming.Stream(key, Stream2Screen())
    stream.filter(track=['de'], languages=['es'])
    # stream.sample()
    


Answer: It seems to be a connection error.

Install pyOpenSSL.

<https://pypi.python.org/pypi/pyOpenSSL>

