Requests: BadStatusLine

Created on 4 Jun 2016  路  2Comments  路  Source: psf/requests

Hi,

python version: 3.4.4
requests version: 2.10.0

I am trying to connect to what has been described as 'ssl socket (normal) with a CRLF json protocol', this means the raw response looks like this:

reply: '{"op":"connection","connectionId":"002-040616070341-5494"}\r\n'

However requests is expecting it to look like this:

reply: 'HTTP/1.1 200 OK\r\n'

Code used:

url = 'https://stream-api.betfair.com:443/api'
session = requests.session()

req = requests.Request('GET', url)
pre = req.prepare()
login_req = session.send(pre, stream=True)

I can see that the error is occurring in _read_status, does anyone know how I can bypass this? Or should I be connecting to this socket differently?

Traceback (most recent call last):
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 385, in _make_request
    httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen
    chunked=chunked)
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 387, in _make_request
    httplib_response = conn.getresponse()
  File "/Users/temp/anaconda/lib/python3.4/http/client.py", line 1227, in getresponse
    response.begin()
  File "/Users/temp/anaconda/lib/python3.4/http/client.py", line 386, in begin
    version, status, reason = self._read_status()
  File "/Users/temp/anaconda/lib/python3.4/http/client.py", line 368, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: {"op":"connection","connectionId":"002-040616071750-5507"}


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/adapters.py", line 403, in send
    timeout=timeout
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 623, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/packages/urllib3/util/retry.py", line 255, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/packages/urllib3/packages/six.py", line 309, in reraise
    raise value.with_traceback(tb)
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen
    chunked=chunked)
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 387, in _make_request
    httplib_response = conn.getresponse()
  File "/Users/temp/anaconda/lib/python3.4/http/client.py", line 1227, in getresponse
    response.begin()
  File "/Users/temp/anaconda/lib/python3.4/http/client.py", line 386, in begin
    version, status, reason = self._read_status()
  File "/Users/temp/anaconda/lib/python3.4/http/client.py", line 368, in _read_status
    raise BadStatusLine(line)
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine('{"op":"connection","connectionId":"002-040616071750-5507"}\r\n',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "marketstreaming/test.py", line 52, in <module>
    login_req = session.send(pre, stream=True)
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "/Users/temp/anaconda/lib/python3.4/site-packages/requests/adapters.py", line 453, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('{"op":"connection","connectionId":"002-040616071750-5507"}\r\n',))

Thanks for any help

Most helpful comment

Requests is a HTTP library: it is not expected to be able to handle arbitrary protocols. In this case, most of the things requests gives you (easy access to HTTP headers and status codes, cookie support, redirect following, etc.) literally don't apply here because there is no HTTP. To do this you'll need to learn the socket library.

All 2 comments

This API does not speak HTTP. You would have to create a custom transport adapter. (Personally I think shoehorning this into requests will be quite painful, better create a custom library).
They should not reuse the well known port for HTTP.

Requests is a HTTP library: it is not expected to be able to handle arbitrary protocols. In this case, most of the things requests gives you (easy access to HTTP headers and status codes, cookie support, redirect following, etc.) literally don't apply here because there is no HTTP. To do this you'll need to learn the socket library.

Was this page helpful?
0 / 5 - 0 ratings