0

Here is the my ssl-socket code:

import ssl, socket

data = b'GET / HTTP/1.1\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nHost: www.google.com.hk\r\nUser-Agent: HTTPie/0.9.8\r\n\r\n'

context = ssl._create_default_https_context()
context.post_handshake_auth = True
context.check_hostname = True

sock = socket.create_connection(('www.google.com.hk', 443), 5, None)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

sslsock = context.wrap_socket(sock, server_hostname='www.google.com.hk')
sslsock.sendall(data)
while rv := sslsock.recv(1000):                                                                         
    print(rv.decode('utf8', errors='ignore'))

Through above code, I could get plain text of 200 OK headers of response, but the response body is till encrypted. I don't know why it is so. And I have been thinking I do exactly the same as builtin package http.client.

For example the code works well using http.client:

import http.client
conn = http.client.HTTPSConnection("www.google.com.hk")
conn.request("GET", "/", headers={'Host': 'www.google.com.hk', 'User-Agent': 'HTTPie/0.9.8'})
while rv := conn.sock.recv(1000):
    print(rv.decode('utf8', 'ignore'))

Please point out why my ssl-socket don't work and how to correct it

yixuan
  • 375
  • 2
  • 17
  • 2
    In short: it is not encrypted. It is compressed since you explicitly said that you would accept gzip compressed content. If you say so you should be able to deal with it too. – Steffen Ullrich Dec 15 '22 at 09:43

0 Answers0