generated from pykit3/tmpl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
synopsis.py
62 lines (48 loc) · 1.33 KB
/
synopsis.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
import k3http
import urllib
import socket
headers = {
'Host': '127.0.0.1',
'Accept-Language': 'en, mi',
}
try:
h = k3http.Client('127.0.0.1', 80)
# send http reqeust without body
# read response status line
# read response headers
h.request('/test.txt', method='GET', headers=headers)
status = h.status
# response code return from http server, type is int
# 200
# 302
# 404
# ...
res_headers = h.headers
# response headers except status line
# res_headers = {
# 'Content-Type': 'text/html;charset=utf-8',
# 'Content-Length': 1024,
# ...
# }
# get response body
print(h.read_body(None))
except (socket.error, k3http.HttpError) as e:
print(repr(e))
content = urllib.urlencode({'f': 'foo', 'b': 'bar'})
headers = {
'Host': 'www.example.com',
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'Content-Length': len(content),
}
try:
h = k3http.Client('127.0.0.1', 80)
# send http reqeust
h.send_request('http://www.example.com', method='POST', headers=headers)
# send http request body
h.send_body(content)
# read response status line and headers
status, headers = h.read_response()
# read response body
print(h.read_body(None))
except (socket.error, k3http.HttpError) as e:
print(repr(e))