Last active
July 3, 2016 12:33
-
-
Save pawelmhm/3aa7e4f3a0e322364dcb75e3f0a32da4 to your computer and use it in GitHub Desktop.
Revisions
-
pawelmhm revised this gist
Jul 3, 2016 . 1 changed file with 61 additions and 117 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,142 +1,86 @@ 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' -
pawelmhm revised this gist
Jul 3, 2016 . 1 changed file with 41 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # DATASET: https://drive.google.com/file/d/0B6myg3n6dqcVcXpPdkJCNUJLOTA/view?usp=sharing import json import sys @@ -99,4 +101,42 @@ def render_GET(self, request): # > openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days XXX 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:10:01+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' """ -
pawelmhm created this gist
Jul 3, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,102 @@ import json import sys from twisted.internet.ssl import DefaultOpenSSLContextFactory from twisted.web import server, resource from twisted.internet import reactor from twisted.python import log from jinja2 import Template from twisted.web.resource import Resource index_template = """ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>bookstore</title> </head> <body> <div class="row"> <div class='col-md-8'> <table class="table"> <th> title </th> <th> author </th> {% for book in books %} <tr> <td> <a href="/book?id={{book._id}}">{{ book.title }}</a> </td> <td> {{book.author}} </td> </tr> {% endfor %} </table> </div> </div> </body> </html> """ book_template = """ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>bookstore</title> </head> <body> <div class="row"> <div class='col-md-8'> <h2> {{book.title}} </h2> <p> {{book.author}} </p> </div> </div> </body> </html> """ class Simple(Resource): isLeaf = True def __init__(self): resource.Resource.__init__(self) with open("reads_id.json") as stock_file: self.books = {} books = json.load(stock_file) for book in books[:1000]: _id = book["_id"] self.books[_id] = book def render_GET(self, request): if request.path == b"/": return Template(index_template).render(books=self.books.values()).encode("utf8") elif request.path == b"/book": book_id = request.args.get(b"id") try: book = self.books.get(int(book_id[0])) except: book = None if not book: request.setResponseCode(404) return b"" return Template(book_template).render(book=book).encode("utf8") return b"" root = Simple() 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 XXX context_factory = DefaultOpenSSLContextFactory("key.pem", "cert.pem") reactor.listenSSL(8080, site, context_factory) reactor.run()