python spdy库位于spdylay c库之上
python-spdyla的Python项目详细描述
Python spdylay是位于spdylay C之上的Python SPDY库 图书馆。支持spdy/2和spdy/3协议。
它不执行任何I/O操作当图书馆需要的时候, 它调用应用程序提供的回调函数它也 不包含任何事件轮询机制,因此应用程序可以 自由选择处理事件的方式
它提供了几乎所有API Spdylay提供的Pythonic时尚
核心库api与python 2和3一起工作。但是 ThreadedSPDYServer需要python 3.3,因为它使用tls npn 分机
安装
首先安装Spdylay库你可以从 sf.net download page 或者clone git repository。
请参见Spdylay documentation了解 必需的包以及如何从git存储库构建Spdylay
安装spdylay后,运行build_ext命令生成 扩展模块:
$ python setup.py build_ext
如果在非标准位置安装了Spdylay库,请使用 --include-dirs和--library-dirs指定头文件和 图书馆的位置
样品
这是一个简单的spdy服务器:
#!/usr/bin/env python # The example SPDY server. Python 3.3 or later is required because TLS # NPN is used in spdylay.ThreadedSPDYServer. Put private key and # certificate file in the current working directory. import spdylay # private key file KEY_FILE='server.key' # certificate file CERT_FILE='server.crt' class MySPDYRequestHandler(spdylay.BaseSPDYRequestHandler): def do_GET(self): self.send_response(200) self.send_header('content-type', 'text/html; charset=UTF-8') content = '''\ <html> <head><title>SPDY FTW</title></head> <body> <h1>SPDY FTW</h1> <p>The age of HTTP/1.1 is over. The time of SPDY has come.</p> </body> </html>'''.encode('UTF-8') self.wfile.write(content) if __name__ == "__main__": HOST, PORT = "localhost", 3000 server = spdylay.ThreadedSPDYServer((HOST, PORT), MySPDYRequestHandler, cert_file=CERT_FILE, key_file=KEY_FILE) server.start()
这是一个简单的spdy客户端:
#!/usr/bin/env python # The example SPDY client. You need Python 3.3 or later because we # use TLS NPN. # # Usage: spdyclient.py URL... # import sys import spdylay class MyStreamHandler(spdylay.BaseSPDYStreamHandler): def on_header(self, nv): sys.stdout.write('Stream#{}\n'.format(self.stream_id)) for k, v in nv: sys.stdout.write('{}: {}\n'.format(k, v)) def on_data(self, data): sys.stdout.write('Stream#{}\n'.format(self.stream_id)) sys.stdout.buffer.write(data) def on_close(self, status_code): sys.stdout.write('Stream#{} closed\n'.format(self.stream_id)) if __name__ == '__main__': uris = sys.argv[1:] spdylay.urlfetch(uris, MyStreamHandler)