Understanding Python HTTP streaming
As verbsintransit has stated, you need to solve your authentication problems, your streaming problems however can be fixed by using this example:
s = requests.Session()
def streaming(symbols):
payload = {'symbols': ','.join(symbols)}
headers = {'connection': 'keep-alive', 'content-type': 'application/json', 'x-powered-by': 'Express', 'transfer-encoding': 'chunked'}
req = requests.Request("GET",'https://stream.tradeking.com/v1/market/quotes.json',
headers=headers,
params=payload).prepare()
resp = s.send(req, stream=True)
for line in resp.iter_lines():
if line:
yield line
def read_stream():
for line in streaming(['AAPL', 'GOOG']):
print line
read_stream()
The if line:
condition is checking if the line
is an actual message or just a connection keep-alive.
Not sure if you figured this out, but TradeKing doesn't put newlines in between their JSON blobs. You thus have to use iter_content to get it byte by byte, append that byte to a buffer, try to decode the buffer, on success clear the buffer and yield the resultant object. :(