You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, some web supports HTTP/2 but doesn't advertise this via ALPN. How can I send HTTP/2 requests using h2.
I failed with the following code
return
<ConnectionTerminated error_code:ErrorCodes.CONNECT_ERROR, last_stream_id:0, additional_data:None>
#!/usr/bin/env python3"""plain_sockets_client.py~~~~~~~~~~~~~~~~~~~~~~~Just enough code to send a GET request via h2 to an HTTP/2 server and receive a response body.This is *not* a complete production-ready HTTP/2 client!"""importsocketimportsslimportcertifiimporth2.connectionimporth2.events"""plain_sockets_client.py~~~~~~~~~~~~~~~~~~~~~~~Just enough code to send a GET request via h2 to an HTTP/2 server and receive a response body.This is *not* a complete production-ready HTTP/2 client!"""importsocketimportsslimportcertifiimporth2.connectionimporth2.eventsSERVER_NAME='0abf00ad04d99ef8c09562b200910005.web-security-academy.net'SERVER_PORT=443# generic socket and ssl configurationsocket.setdefaulttimeout(15)
ctx=ssl.create_default_context(cafile=certifi.where())
ctx.set_alpn_protocols(['h2'])
# open a socket to the server and initiate TLS/SSLs=socket.create_connection((SERVER_NAME, SERVER_PORT))
s=ctx.wrap_socket(s, server_hostname=SERVER_NAME)
c=h2.connection.H2Connection()
c.initiate_connection()
s.sendall(c.data_to_send())
headers= [
(':method', 'GET'),
(':path', '/reqinfo'),
(':authority', SERVER_NAME),
(':scheme', 'https'),
]
c.send_headers(1, headers, end_stream=True)
s.sendall(c.data_to_send())
body=b''response_stream_ended=Falsewhilenotresponse_stream_ended:
# read raw data from the socketdata=s.recv(65536*1024)
ifnotdata:
break# feed raw data into h2, and process resulting eventsevents=c.receive_data(data)
foreventinevents:
print(event)
ifisinstance(event, h2.events.DataReceived):
# update flow control so the server doesn't starve usc.acknowledge_received_data(event.flow_controlled_length, event.stream_id)
# more response body data receivedbody+=event.dataifisinstance(event, h2.events.StreamEnded):
# response body completed, let's exit the loopresponse_stream_ended=Truebreak# send any pending data to the servers.sendall(c.data_to_send())
print("Response fully received:")
print(body.decode())
# tell the server we are closing the h2 connectionc.close_connection()
s.sendall(c.data_to_send())
# close the sockets.close()
SERVER_PORT=443# generic socket and ssl configurationsocket.setdefaulttimeout(15)
ctx=ssl.create_default_context(cafile=certifi.where())
ctx.set_alpn_protocols(['h2'])
# open a socket to the server and initiate TLS/SSLs=socket.create_connection((SERVER_NAME, SERVER_PORT))
s=ctx.wrap_socket(s, server_hostname=SERVER_NAME)
c=h2.connection.H2Connection()
c.initiate_connection()
s.sendall(c.data_to_send())
headers= [
(':method', 'GET'),
(':path', '/reqinfo'),
(':authority', SERVER_NAME),
(':scheme', 'https'),
]
c.send_headers(1, headers, end_stream=True)
s.sendall(c.data_to_send())
body=b''response_stream_ended=Falsewhilenotresponse_stream_ended:
# read raw data from the socketdata=s.recv(65536*1024)
ifnotdata:
break# feed raw data into h2, and process resulting eventsevents=c.receive_data(data)
foreventinevents:
print(event)
ifisinstance(event, h2.events.DataReceived):
# update flow control so the server doesn't starve usc.acknowledge_received_data(event.flow_controlled_length, event.stream_id)
# more response body data receivedbody+=event.dataifisinstance(event, h2.events.StreamEnded):
# response body completed, let's exit the loopresponse_stream_ended=Truebreak# send any pending data to the servers.sendall(c.data_to_send())
print("Response fully received:")
print(body.decode())
# tell the server we are closing the h2 connectionc.close_connection()
s.sendall(c.data_to_send())
# close the sockets.close()
The text was updated successfully, but these errors were encountered:
Hello, some web supports HTTP/2 but doesn't advertise this via ALPN. How can I send HTTP/2 requests using h2.
I failed with the following code
return
<ConnectionTerminated error_code:ErrorCodes.CONNECT_ERROR, last_stream_id:0, additional_data:None>
The text was updated successfully, but these errors were encountered: