forked from zjzslsc/Python-Raw-Socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.py
83 lines (69 loc) · 1.74 KB
/
http.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
from transport import TcpSocket
from urlparse import urlparse
from time import time
RES_TIMEOUT = 10
def form_get(path, host):
line = ''
line += 'GET ' + path + ' HTTP/1.1\r\n'
line += 'Host: ' + host + '\r\n'
line += 'Connection: keep-alive\r\n'
line += 'Accept: text/html\r\n'
line += 'Accept-Encoding: gzip\r\n'
#line += 'Connection: Keep-Alive\r\n'
line += '\r\n'
return line
s = TcpSocket()
address = sys.argv[1]
if address[:7] == 'http://':
pass
else:
address = 'http://'+address
o = urlparse(address)
s.connect(o.netloc)
path = ''
foo = ''
if o.path == '':
path = '/'
else:
path = o.path
data = ''
s.send(form_get(path, o.netloc))
clen = 0
header_received = False
st_time = time()
while 1:
'control receive timeout'
if time() - st_time > RES_TIMEOUT:
print 'Socket timeout when requesting http.'
sys.exit(0)
'receive header from socket'
data += s.recv()
if data.find('\r\n\r\n') > -1 and not header_received:
header_received = True
tmp = data[0:data.find('\r\n\r\n')].split('\r\n')
for line in tmp:
if 'Content-Length:' in line:
clen = int(line.replace('Content-Length: ', ''))
data = data[data.find('\r\n\r\n')+4:]
#print 'content-length = %d' % (clen)
if header_received:
if len(data) >= clen:
data = data[:clen]
break
from cStringIO import StringIO
from gzip import GzipFile
data = GzipFile(fileobj=StringIO(data), mode='r').read()
#print data
s.close()
tmp = path.split('/')
if tmp[-1] == '':
foo = 'index.html'
else:
if '.html' in tmp[-1]:
foo = tmp[-1]
else:
foo = tmp[-1]+'.html'
f = open(foo, 'w')
f.write(data)
f.close()