if __name__ == '__main__': host, port = parse_args() out_dir = os.path.join(os.getcwd(), host) if not os.path.exists(out_dir): try: os.mkdir(out_dir) except OSError: out_dir = mkdtemp() print('Using %s as output directory' % (out_dir)) print('Trying to connect to %s:%i' % (host, port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port)) connection = TLSConnection(sock) connection.handshakeClientCert(nextProtos=["spdy/%i" % SPDY_VERSION]) print ('TLS NPN Selected: %s' % connection.next_proto) spdy_ctx = Context(CLIENT, version=SPDY_VERSION) connection.write(get_site_resource(spdy_ctx, host)) headers = {spdy_ctx._last_stream_id: {'path': 'index'}} res_files = {spdy_ctx._last_stream_id: (os.path.join(out_dir, 'index'), None)} resources = None goaway = False while not goaway: answer = connection.read() # Blocking spdy_ctx.incoming(answer) frame = get_frame(spdy_ctx) while frame: sid = getattr(frame, 'stream_id', None) if hasattr(frame, 'headers'): print ('<<', frame, 'Headers:', frame.headers) # DEBUG if sid not in headers: headers[sid] = {}
if __name__ == '__main__': host, port = parse_args() out_dir = os.path.join(os.getcwd(), host) if not os.path.exists(out_dir): try: os.mkdir(out_dir) except OSError: out_dir = mkdtemp() print('Using %s as output directory' % (out_dir)) print('Trying to connect to %s:%i' % (host, port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port)) connection = TLSConnection(sock) connection.handshakeClientCert(nextProtos=["spdy/%i" % SPDY_VERSION]) print('TLS NPN Selected: %s' % connection.next_proto) spdy_ctx = Context(CLIENT, version=SPDY_VERSION) connection.write(get_site_resource(spdy_ctx, host)) headers = {spdy_ctx._last_stream_id: {'path': 'index'}} res_files = { spdy_ctx._last_stream_id: (os.path.join(out_dir, 'index'), None) } resources = None goaway = False while not goaway: answer = connection.read() # Blocking spdy_ctx.incoming(answer) frame = get_frame(spdy_ctx) while frame: sid = getattr(frame, 'stream_id', None) if hasattr(frame, 'headers'): print('<<', frame, 'Headers:', frame.headers) # DEBUG
TODO: Doesn't work in python 2, remedy this """ hexa = '' for s in string: hexa += '0x%02x' % s + ' ' return hexa.rstrip() def print_encoded_frame(frame): for i in range(0, len(frame), 4): print(str2hexa(frame[i:i + 4])) if __name__ == '__main__': ctx = Context(CLIENT) frame = SynStream(1, headers={ 'method': 'GET', 'url': '/', 'version': 'HTTP/1.1', 'host': 'www.google.com', 'scheme': 'https', }) print('SYN Frame') byte_frame = ctx._encode_frame(frame) print_encoded_frame(byte_frame) frame2 = ctx._parse_frame(byte_frame)[0] print('decoded: %s, %r' % (frame2, frame.headers)) print('SETTINGS Frame')
try: return spdy_ctx.get_frame() except SpdyProtocolError as e: print ('error parsing frame: %s' % str(e)) if __name__ == '__main__': host, port = parse_args() print('Trying to connect to %s:%i' % (host, port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port)) connection = TLSConnection(sock) connection.handshakeClientCert(nextProtos=["spdy/%i" % SPDY_VERSION]) print ('TLS NPN Selected: %r' % connection.next_proto) spdy_ctx = Context(CLIENT, version=SPDY_VERSION) ping_test(spdy_ctx) get_page(spdy_ctx, host) out = spdy_ctx.outgoing() #print str2hexa(str(out)) connection.write(out) file_out = open('/tmp/spdyout.txt', 'wb') goaway = False content_type_id = {} while not goaway: answer = connection.read() # Blocking #print '<<\n', str2hexa(answer) spdy_ctx.incoming(answer) frame = get_frame(spdy_ctx) while frame: if hasattr(frame, 'headers'):
conn.put_frame(resp) print(str(resp) + ", SAYS SERVER") data = DataFrame(f.stream_id, b"hello, world!", flags=1) conn.put_frame(data) print(str(data) + ", SAYS SERVER") goaway = Goaway(f.stream_id) conn.put_frame(goaway) print(str(goaway) + ", SAYS SERVER") elif isinstance(f, Goaway): return True try: print ('Running one-time one-client SPDY Server...') client_socket, address = server.accept() ss = ctx.wrap_socket(client_socket, server_side=True) conn = Context(SERVER) finish = False while not finish: d = ss.recv(1024) conn.incoming(d) frame = conn.get_frame() if frame: finish = handle_frame(conn, frame) outgoing = conn.outgoing() if outgoing: ss.sendall(outgoing) except Exception as exc: print(exc) finally:
#!/usr/bin/env python # coding: utf-8 from spdy.context import Context, CLIENT, SERVER from spdy.frames import SynStream, SynReply server = Context(SERVER) client = Context(CLIENT) frame = SynStream(stream_id=client.next_stream_id, headers={'dood': 'balls', 'stuff': 'otherstuff'}) client.put_frame(frame) chunk = client.outgoing() server.incoming(chunk) frame2 = server.get_frame() print(frame2) print(frame2.headers) frame3 = SynReply(stream_id=server.next_stream_id, headers={'got it': 'yup', 'roger': 'roger'}) server.put_frame(frame3) chunk2 = server.outgoing() client.incoming(chunk2) frame4 = client.get_frame() print(frame4.headers)
In [5]: str2hexa('abc\n') Out[5]: '0x61 0x62 0x63 0x0A' TODO: Doesn't work in python 2, remedy this """ hexa='' for s in string: hexa += '0x%02x' % s + ' ' return hexa.rstrip() def print_encoded_frame(frame): for i in range(0, len(frame), 4): print(str2hexa(frame[i:i+4])) if __name__ == '__main__': ctx = Context(CLIENT) frame = SynStream(1, headers={'method' : 'GET', 'url' : '/', 'version': 'HTTP/1.1', 'host' : 'www.google.com', 'scheme' : 'https', }) print ('SYN Frame') byte_frame = ctx._encode_frame(frame) print_encoded_frame(byte_frame) frame2 = ctx._parse_frame(byte_frame)[0] print('decoded: %s, %r' % (frame2, frame.headers)) print ('SETTINGS Frame') frame = Settings(2, {UPLOAD_BANDWIDTH: (PERSIST_NONE, 60), DOWNLOAD_BANDWIDTH : (PERSIST_NONE, 128)})
print(str(resp) + ", SAYS SERVER") data = DataFrame(f.stream_id, b"hello, world!", flags=1) conn.put_frame(data) print(str(data) + ", SAYS SERVER") goaway = Goaway(f.stream_id) conn.put_frame(goaway) print(str(goaway) + ", SAYS SERVER") elif isinstance(f, Goaway): return True try: print('Running one-time one-client SPDY Server...') client_socket, address = server.accept() ss = ctx.wrap_socket(client_socket, server_side=True) conn = Context(SERVER) finish = False while not finish: d = ss.recv(1024) conn.incoming(d) frame = conn.get_frame() if frame: finish = handle_frame(conn, frame) outgoing = conn.outgoing() if outgoing: ss.sendall(outgoing) except Exception as exc: print(exc) finally: server.close()