Skip to content

Instantly share code, notes, and snippets.

@pawelmhm
Last active July 3, 2016 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelmhm/3aa7e4f3a0e322364dcb75e3f0a32da4 to your computer and use it in GitHub Desktop.
Save pawelmhm/3aa7e4f3a0e322364dcb75e3f0a32da4 to your computer and use it in GitHub Desktop.
Twisted resource to test HTTP2
import json
import sys
from twisted.internet.ssl import DefaultOpenSSLContextFactory
from twisted.web import server
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.python import log
# Get datafile here:
# https://drive.google.com/file/d/0B6myg3n6dqcVcXpPdkJCNUJLOTA/view?pref=2&pli=1
def load_stock():
stock = {}
with open("reads_id.json") as stock_file:
books = json.load(stock_file)
for book in books:
_id = str(book["_id"])
stock[_id] = book
return stock
BOOKS = load_stock()
class Index(Resource):
def render_GET(self, request):
return json.dumps(BOOKS).encode("utf8")
class Book(Resource):
isLeaf = True
def render_GET(self, request):
book_id = request.args.get(b"id")
book = BOOKS.get(book_id[0].decode("utf8"))
if not book:
request.setResponseCode(404)
return b""
return json.dumps(book).encode("utf8")
root = Resource()
root.putChild(b"", Index())
root.putChild(b"book", Book())
site = server.Site(root)
log.startLogging(sys.stdout)
# get keys with following command:
# > openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 90 -nodes
context_factory = DefaultOpenSSLContextFactory("key.pem", "cert.pem")
reactor.listenSSL(8080, site, context_factory)
reactor.run()
# > curl2 --http2 "https://localhost:8080/" -v --cacert cert.pem
# 2016-07-03 14:27:14+0200 [_GenericHTTPChannelProtocol (TLSMemoryBIOProtocol),0,127.0.0.1] Unhandled Error
# Traceback (most recent call last):
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/python/log.py", line 101, in callWithLogger
# return callWithContext({"system": lp}, func, *args, **kw)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/python/log.py", line 84, in callWithContext
# return context.call({ILogContext: newCtx}, func, *args, **kw)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/python/context.py", line 118, in callWithContext
# return self.currentContext().callWithContext(ctx, func, *args, **kw)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/python/context.py", line 81, in callWithContext
# return func(*args,**kw)
# --- <exception caught here> ---
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
# why = selectable.doRead()
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/internet/tcp.py", line 209, in doRead
# return self._dataReceived(data)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
# rval = self.protocol.dataReceived(data)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/protocols/tls.py", line 430, in dataReceived
# self._flushReceiveBIO()
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/protocols/tls.py", line 400, in _flushReceiveBIO
# ProtocolWrapper.dataReceived(self, bytes)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/protocols/policies.py", line 120, in dataReceived
# self.wrappedProtocol.dataReceived(data)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/web/http.py", line 2320, in dataReceived
# return self._channel.dataReceived(data)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/web/_http2.py", line 166, in dataReceived
# self._handleWindowUpdate(event)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/twisted/web/_http2.py", line 581, in _handleWindowUpdate
# self.priority.unblock(streamID)
# File "/home/pawel/.virtualenvs/http2/lib/python3.4/site-packages/priority/priority.py", line 359, in unblock
# raise MissingStreamError("Stream %d not in tree" % stream_id)
# priority.priority.MissingStreamError: 'Stream 1 not in tree'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment