Understanding Python HTTP Streaming

I am trying to access the streaming API using Python and queries.

What the API says: "Weve turned on the streaming endpoint to request both quotation data and trade data using a permanent HTTP socket connection. Streaming data from the API is to execute an authenticated HTTP request and leave the HTTP open connection to constantly receive data. "

How I tried to access the data:

s = requests.Session() def streaming(symbols): url = 'https://stream.tradeking.com/v1/market/quotes.json' payload = {'symbols': ','.join(symbols)} return s.get(url, params=payload, stream=True) r = streaming(['AAPL', 'GOOG']) 

The docs requests here show two things of interest: use a generator / iterator for use with interleaved data passed into the data field. It is proposed to use code for streaming data, for example:

 for line in r.iter_lines(): print(line) 

Nothing works, although I have no idea what to turn on the generator function, as the example is unclear. Using r.iter_lines (), I get the output: "b" {"status": "connected"} {"status": disconnected "} '"

I can access the headers and the response is HTTP 200, but cannot get valid data or find clear examples of how to access HTTP streaming data in python. Any help would be greatly appreciated. The API recommends using Jetty for Java to keep the stream open, but I'm not sure how to do this in Python.

Headers: {'connection': 'keep-alive', 'content-type': 'application / json', 'x-powered-by': 'Express', 'transfer-encoding': 'chunked'}

+4
source share
2 answers

Not sure if you get it, but TradeKing doesn't put new lines between their JSON blocks. So you have to use iter_content to get it byte by byte, add this byte to the buffer, try to decode the buffer, if successful, clear the buffer and give the resulting object .: (

+2
source

As verbsintransit said, you need to solve your authentication problems, however your streaming problems can be fixed with 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() 

if line: condition if line: checks if line actual message or if only the connection is supported.

+9
source

Source: https://habr.com/ru/post/1493068/


All Articles