Server IP : 85.214.239.14 / Your IP : 18.227.140.100 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /usr/share/doc/python3-h2/examples/plain_sockets/ |
Upload File : |
#!/usr/bin/env python3 """ plain_sockets_client.py ~~~~~~~~~~~~~~~~~~~~~~~ Just enough code to send a GET request via h2 to an HTTP/2 server and receive a response body. This is *not* a complete production-ready HTTP/2 client! """ import socket import ssl import certifi import h2.connection import h2.events SERVER_NAME = 'http2.golang.org' SERVER_PORT = 443 # generic socket and ssl configuration socket.setdefaulttimeout(15) ctx = ssl.create_default_context(cafile=certifi.where()) ctx.set_alpn_protocols(['h2']) # open a socket to the server and initiate TLS/SSL s = socket.create_connection((SERVER_NAME, SERVER_PORT)) s = ctx.wrap_socket(s, server_hostname=SERVER_NAME) c = h2.connection.H2Connection() c.initiate_connection() s.sendall(c.data_to_send()) headers = [ (':method', 'GET'), (':path', '/reqinfo'), (':authority', SERVER_NAME), (':scheme', 'https'), ] c.send_headers(1, headers, end_stream=True) s.sendall(c.data_to_send()) body = b'' response_stream_ended = False while not response_stream_ended: # read raw data from the socket data = s.recv(65536 * 1024) if not data: break # feed raw data into h2, and process resulting events events = c.receive_data(data) for event in events: print(event) if isinstance(event, h2.events.DataReceived): # update flow control so the server doesn't starve us c.acknowledge_received_data(event.flow_controlled_length, event.stream_id) # more response body data received body += event.data if isinstance(event, h2.events.StreamEnded): # response body completed, let's exit the loop response_stream_ended = True break # send any pending data to the server s.sendall(c.data_to_send()) print("Response fully received:") print(body.decode()) # tell the server we are closing the h2 connection c.close_connection() s.sendall(c.data_to_send()) # close the socket s.close()