initialstate / pi-sensor-free-presence-detector

Know Who's at the Office with the Raspberry Pi
MIT License
368 stars 56 forks source link

ship: ISStreamer failed to ship after a number of attempts. #6

Open miguelmota opened 6 years ago

miguelmota commented 6 years ago
$ python is_example.py
ship: ISStreamer failed to ship after a number of attempts.
ship: ISStreamer failed to ship after a number of attempts.
ship: ISStreamer failed to ship after a number of attempts.
ship: ISStreamer failed to ship after a number of attempts.
ship: ISStreamer failed to ship after a number of attempts.
ship: ISStreamer failed to ship after a number of attempts.
Exception in thread Thread-4:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/Library/Python/2.7/site-packages/ISStreamer/Streamer.py", line 253, in __ship_buffer
    self.ship_messages(messages)
  File "/Library/Python/2.7/site-packages/ISStreamer/Streamer.py", line 186, in ship_messages
    self.ship_to_api("/api/events", messages)
  File "/Library/Python/2.7/site-packages/ISStreamer/Streamer.py", line 160, in ship_to_api
    __ship(3)
  File "/Library/Python/2.7/site-packages/ISStreamer/Streamer.py", line 157, in __ship
    raise ex
ValueError: I/O operation on closed file
RChloe commented 6 years ago

This response is usually due to a network error - can you confirm that you have an internet connection by running ping www.google.com? If you have a good connection, you should see: PING google.com (216.58.218.14): 56 data bytes 64 bytes from 216.58.218.14: icmp_seq=0 ttl=55 time=9.100 ms

charliechaf commented 4 years ago

While following the basic tutorial and running from ISStreamer.Streamer import Streamer streamer = Streamer(bucket_name="", bucket_key="", access_key="", debug_level=2) streamer.log("test", "hi") streamer.log("temperature", 32) streamer.close() I was getting the ship: ISStreamer failed to ship after a number of attempts error only when using a Jupyter notebook. I switched to PyCharm and the same code worked perfectly.

davidsulpy commented 4 years ago

@charliechaf and when you tried it in the Jupyter notebook, did you ensure your bucket_key and access_key where properly set?

charliechaf commented 4 years ago

@davidsulpy yes, I'm sure. I just tested it again. The streamer works perfectly in Pycharm but when running in Jupyter it gives me the error.

davidsulpy commented 4 years ago

@charliechaf can you paste your entire output from running this? I just verified in my own jupyter notebook and it looks like it works well for me, see the attached screenshot

Screenshot 2019-08-15 15 22 20
charliechaf commented 4 years ago

yep. Here is the screenshot with the error message.

Screen Shot 2019-08-16 at 10 14 32 AM
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 130, in __ship
    conn.request('POST', resource, json.dumps(contents), headers)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1392, in connect
    server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 130, in __ship
    conn.request('POST', resource, json.dumps(contents), headers)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1392, in connect
    server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 130, in __ship
    conn.request('POST', resource, json.dumps(contents), headers)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1392, in connect
    server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 177, in __create_bucket
    self.ship_to_api("/api/buckets", {'bucketKey': new_bucket_key, 'bucketName': new_bucket_name})
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 173, in ship_to_api
    __ship(3)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 171, in __ship
    __ship(retry_attempts, 1)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 171, in __ship
    __ship(retry_attempts, 1)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 171, in __ship
    __ship(retry_attempts, 1)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ISStreamer/Streamer.py", line 122, in __ship
    self.MissedEvents.write("{}\n".format(json.dumps(contents)))
ValueError: I/O operation on closed file.
davidsulpy commented 4 years ago

Hey @charliechaf, thanks for pasting, that's very helpful. The error that you're getting is related to the TLS negotiation that the library does through python under the hood.

The machine running your jupyter book may simply not have a cert_bundle or the one that it has is specialized or old. Initial State's streaming endpoint uses AWS as the certificate authority, so you should be able to solve this by downloading the public certs for AWS CA Roots and reference them in your system before running the python.

Here is a link to the AWS Certs: https://www.amazontrust.com/repository/ Here is a link to Amazon Root CA 1 (which is the root of our cert chain): https://www.amazontrust.com/repository/AmazonRootCA1.pem And here is a link to a RedHat article that explains some ways to test this and why it happens: https://access.redhat.com/articles/2039753

Specifically, they use this code:

#!/usr/bin/env python

try:   
    import urllib2 #python2
except:
    import urllib.request as urllib2 #python3
import sys

req = urllib2.Request(sys.argv[1], headers={'User-Agent':'Mozilla/5.0'})
urllib2.urlopen(req)

and this command to test:

$ SSL_CERT_FILE=/etc/rhsm/ca/redhat-uep.pem python urllib2-test.py https://cdn.redhat.com/

You could simply replace the redhat.com url with this url to test our streaming endpoint directly:

$ SSL_CERT_FILE=/etc/rhsm/ca/redhat-uep.pem python urllib2-test.py https://groker.init.st/version