import socket import ssl headers Accept text html application xhtml xm

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import socket
import ssl
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "",#gzip, deflate",
"Accept-Language": "en-GB,en;q=0.9,ru-RU;q=0.8,ru;q=0.7,en-US;q=0.6",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
}
hostname = 'www.amazon.com'
ctx = ssl.create_default_context()
with socket.create_connection((hostname, 443)) as raw_sock:
with ctx.wrap_socket(raw_sock, server_hostname=hostname) as sock:
sock.sendall(
b'GET /robots.txt HTTP/1.1\r\n'
b'Host: %s\r\n'
b'%s\r\n'
b'\r\n'
% (
hostname.encode(),
('\r\n'.join('%s: %s' % x for x in headers.items())).encode(),
)
)
data = sock.recv(2000)
head, body = data.split(b'\r\n\r\n', 1)
print(head.decode('utf-8'))
print('-' * 10)
print(body.decode('utf-8', 'ignore'))