Last active
May 7, 2016 19:37
-
-
Save greencoder/aa77308abcb006c65b02d5c4787936da to your computer and use it in GitHub Desktop.
Example Hyper Client and H2 Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hyper | |
import json | |
import sys | |
if len(sys.argv) != 4: | |
print('Usage: server.py /path/to/certificate.pem /path/to/key.pem certificate_password') | |
sys.exit(2) | |
cert = sys.argv[1] | |
key = sys.argv[2] | |
password = sys.argv[3] | |
ssl_context = hyper.tls.init_context() | |
ssl_context.load_cert_chain(certfile=cert, keyfile=key, password=password) | |
ssl_context.load_verify_locations(cafile=cert) | |
connection = hyper.HTTP20Connection('localhost', 8443, | |
ssl_context=ssl_context, force_proto='h2', secure=True) | |
data = { | |
"title": "Test Message", | |
"body": "This is a test message", | |
} | |
json_payload = json.dumps(data, ensure_ascii=False, separators=(',', ':')) | |
json_payload = json_payload.encode('utf-8') | |
stream_id = connection.request('POST', '/', json_payload) | |
response = connection.get_response(stream_id) | |
print(response.status) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 300 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start the server | |
$ python server.py cert.pem key.pem pass | |
# Run the client (different window) | |
$ python client.py cert.pem key.pem pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import json | |
import random | |
import ssl | |
import sys | |
import h2 | |
from h2.connection import H2Connection | |
from h2.events import DataReceived, RequestReceived | |
class H2Protocol(asyncio.Protocol): | |
def __init__(self): | |
self.conn = H2Connection(client_side=False) | |
self.transport = None | |
def connection_made(self, transport: asyncio.Transport): | |
self.transport = transport | |
self.conn.initiate_connection() | |
self.transport.write(self.conn.data_to_send()) | |
def data_received(self, data: bytes): | |
events = self.conn.receive_data(data) | |
for event in events: | |
if isinstance(event, RequestReceived): | |
self.request_received(event.stream_id) | |
elif isinstance(event, DataReceived): | |
self.transport.write(self.conn.data_to_send()) | |
def request_received(self, stream_id: int): | |
print("Request received on stream {}", stream_id) | |
data = json.dumps({'status': 'message received'}).encode('utf8') | |
response_headers = ( | |
(':status', '200'), | |
('content-type', 'application/json'), | |
('content-length', len(data)), | |
('server', 'asyncio-h2'), | |
) | |
self.conn.send_headers(stream_id, response_headers) | |
self.conn.send_data(stream_id, data, end_stream=True) | |
if __name__ == '__main__': | |
if len(sys.argv) != 4: | |
print('Usage: server.py /path/to/certificate.pem /path/to/key.pem certificate_password') | |
sys.exit(2) | |
h2.settings.HEADER_TABLE_SIZE = 1 | |
cert = sys.argv[1] | |
key = sys.argv[2] | |
password = sys.argv[3] | |
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) | |
ssl_context.options |= ( | |
ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_COMPRESSION | |
) | |
ssl_context.set_ciphers("ECDHE+AESGCM") | |
ssl_context.load_cert_chain(certfile=cert, keyfile=key, password=password) | |
ssl_context.set_alpn_protocols(["h2"]) | |
loop = asyncio.get_event_loop() | |
# Each client connection will create a new protocol instance | |
coro = loop.create_server(H2Protocol, '127.0.0.1', 8443, ssl=ssl_context) | |
server = loop.run_until_complete(coro) | |
# Serve requests until Ctrl+C is pressed | |
print('Serving on {}. Hit Ctrl+C to stop'.format(server.sockets[0].getsockname())) | |
try: | |
loop.run_forever() | |
except KeyboardInterrupt: | |
pass | |
server.close() | |
loop.run_until_complete(server.wait_closed()) | |
loop.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(propeller)Scotts-iMac-3:example snewman$ python client.py cert.pem key.pem pass | |
Traceback (most recent call last): | |
File "client.py", line 27, in <module> | |
stream_id = connection.request('POST', '/', json_payload) | |
File "/Users/snewman/.Envs/propeller/lib/python3.5/site-packages/hyper/http20/connection.py", line 262, in request | |
self.endheaders(message_body=body, final=True, stream_id=stream_id) | |
File "/Users/snewman/.Envs/propeller/lib/python3.5/site-packages/hyper/http20/connection.py", line 525, in endheaders | |
self.connect() | |
File "/Users/snewman/.Envs/propeller/lib/python3.5/site-packages/hyper/http20/connection.py", line 349, in connect | |
force_proto=self.force_proto) | |
File "/Users/snewman/.Envs/propeller/lib/python3.5/site-packages/hyper/tls.py", line 42, in wrap_socket | |
ssl_sock = _ssl_context.wrap_socket(sock, server_hostname=server_hostname) | |
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 376, in wrap_socket | |
_context=self) | |
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 747, in __init__ | |
self.do_handshake() | |
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 983, in do_handshake | |
self._sslobj.do_handshake() | |
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 628, in do_handshake | |
self._sslobj.do_handshake() | |
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment