-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_client_example.py
More file actions
46 lines (39 loc) · 1.13 KB
/
http_client_example.py
File metadata and controls
46 lines (39 loc) · 1.13 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import urllib.request
def get():
try:
req = urllib.request.Request("http://www.baidu.com/")
res = urllib.request.urlopen(req)
print(res.read().decode('utf-8'))
except Exception as err:
print("get err: {}".format(err))
def post():
try:
req = urllib.request.Request(
"http://zookeeper.apache.org",
data=json.dumps({"test":1, "hello":"world"}).encode('utf-8'),
method='POST'
)
res = urllib.request.urlopen(req)
print(res.read().decode('utf-8'))
except Exception as err:
print("post err: {}".format(err))
def delete():
try:
args = "id={}&domain={}&sub_domain={}".format(
1, "test", "subtest"
)
req = urllib.request.Request(
"http://zookeeper.apache.org/" + args,
method='DELETE'
)
res = urllib.request.urlopen(req)
print(res.read().decode('utf-8'))
except Exception as err:
print("delete err: {}".format(err))
if __name__ == "__main__":
get()
post()
delete()