-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliente https.py
More file actions
67 lines (48 loc) · 1.63 KB
/
Copy pathCliente https.py
File metadata and controls
67 lines (48 loc) · 1.63 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import socket, sys, ssl
def wrapSocket(sock, serverurl):
purpose = ssl.Purpose.SERVER_AUTH
context = ssl.create_default_context(purpose)
return context.wrap_socket(sock, server_hostname=serverurl)
if len(sys.argv) == 4 and sys.argv[2] == "-o":
hostParts = sys.argv[1].split("://")
if not hostParts[0] in ["http", "https"]:
sys.exit(1)
barIndex = hostParts[1].find("/")
HOST = hostParts[1][:barIndex]
resource = hostParts[1][barIndex:]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if hostParts[0] == 'https':
PORTA = 80
if hostParts[0] == "https":
sock = wrapSocket(sock, HOST)
PORTA = 443
sock.connect((HOST, PORTA))
req = ('GET '+resource+' HTTP/1.1\r\n'+
'Host: '+HOST+'\r\n'+
'\r\n').encode()
sock.sendall(req)
resp = sock.recv(4096)
while b'\r\n\r\n' not in resp:
resp += sock.recv(4096)
data = resp.split(b'\r\n\r\n')
headers = data[0]
body = data[1]
headers = headers.split(b'\r\n')
statusLine = headers[0]
headers = headers[1:]
print (statusLine)
print (headers)
toRead = 0
for header in headers:
header = header.split(b':')
if header[0] == b'Content-Length':
toRead = int(header[1])
if header[0] == b'Transfer-Enconding: Chunked':
toRead = int(header[1])
if toRead > 0:
while len(body) < toRead:
body += sock.recv(4096)
fd = open (sys.argv[3], 'wb')
fd.write(body)
fd.close()
sock.close()