python-hyper / hyper

HTTP/2 for Python.
http://hyper.rtfd.org/en/latest/
MIT License
1.05k stars 191 forks source link

Streams example isn't working #294

Closed km-pg closed 7 years ago

km-pg commented 7 years ago

The streams example from the quickstart guide isn't working. I changed it a little, but still can't get it to work:

import ssl
ssl.OP_NO_COMPRESSION = 131072

from hyper import HTTPConnection
c = HTTPConnection('api.segment.io', port=443)
first = c.request('POST', '/v1/batch', body="{}")
second = c.request('POST', '/v1/batch', body="{}")
third = c.request('POST', '/v1/batch', body="{}")
second_response = c.get_response(second)
first_response = c.get_response(first)
third_response = c.get_response(third)

Here's the error I'm getting:

Traceback (most recent call last):
  File "hy.py", line 22, in <module>
    second_response = c.get_response(second)
  File "venv/lib/python2.7/site-packages/hyper/common/connection.py", line 129, in get_response
    return self._conn.get_response(*args, **kwargs)
TypeError: get_response() takes exactly 1 argument (2 given)
Lukasa commented 7 years ago

I suspect if you look you'll find that first, second, and third are all None, meaning that HTTP/2 was never negotiated. The example definitely will not work in that case. =)

km-pg commented 7 years ago

Ah, you are right. I thought it would be HTTP/2 because when you go to that URL in Chrome, the protocol in the Developer Tools shows h2. Is it possible that a GET would be HTTP/2 and a POST would not be?

km-pg commented 7 years ago

I switched to using GET, but it still isn't doing HTTP/2 it seems. Why is it in Chrome by not in hyper?

from hyper import HTTPConnection
c = HTTPConnection('api.segment.io', port=443)
stream_id = c.request('GET', '/v1/batch')
print(stream_id)

returns None

km-pg commented 7 years ago

Nevermind. I'm running into this issue: https://github.com/Lukasa/hyper/issues/178

After installing python with pyenv, it worked.

km-pg commented 7 years ago

Is there a way to tell if the connection is HTTP/2 before sending the first request?

Lukasa commented 7 years ago

Nope: we delay connection setup as long as possible so you can only tell based on whether you have a stream ID or None.