-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
80 lines (63 loc) · 1.79 KB
/
client.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
import socket
from utils import log
def parsed_url(url):
"""
decode url, return protocol,host,port,path
"""
# decode protocol
protocol = 'http'
if url[:7] == 'http://':
u = url.split('://')[1]
elif url[:8] == 'https://':
protocol = 'https'
u = url.split('://')[1]
else:
u = url
# decode path
i = u.find('/')
if i == -1:
host = u
path = '/'
else:
host = u[:i]
path = u[i:]
if host.find(':') != -1:
h = host.split(':')
host = h[0]
port = int(h[1])
else:
# decode port
port_dict = {
'http': 80,
'https': 443,
}
# default port
port = port_dict[protocol]
return protocol, host, port, path
def response_by_socket(s):
"""
:param s: a socket where the response from
:return: a bytes get from socket connection
"""
response = b''
buffer_size = 512
while True:
buffer = s.recv(buffer_size)
if len(buffer) != 0:
response += buffer
else:
return response
def get(url):
protocol, host, port, path = parsed_url(url)
s = socket.socket() # 1st: gain socket ( use ssl.wrap_socket(s) for https)
s.connect((host, port)) # 2nd. connect
ip, port = s.getsockname()
log.i("local ip is {} ,local port is {}".format(ip, port))
# use Connection:close for use buffer when read from socket
request = "GET / HTTP/1.1\r\nhost:{}\r\nConnection: close\r\n\r\n".format(host).encode("utf-8")
s.send(request) # 3rd. send
log.i("request is {}".format(request))
response = response_by_socket(s) # 4th. receive
log.i("response is \n{}".format(response.decode("utf-8")))
if __name__ == '__main__':
get("www.baidu.com")