-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_examples.py
More file actions
160 lines (103 loc) · 3.54 KB
/
web_examples.py
File metadata and controls
160 lines (103 loc) · 3.54 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#urllib
import urllib.request
from http import HTTPStatus
from urllib.error import HTTPError,URLError
# response =urllib.request.urlopen('http://httpbin.org/xml', data=None)
#get
# url = 'http://httpbin.org/get'
# args = {
# "name": "Gbenga Awodokun",
# "engineer": True}
# data = urllib.parse.urlencode(args)
# # response = urllib.request.urlopen(url + "?" + data)
# #POST - encode into bytes
# data = data.encode()
# url = "http://httpbin.org/post"
# response = urllib.request.urlopen(url, data = data)
# print(response.status)
# # #--headers
# # print(response.getheaders())
# # #--body
# print(response.read().decode('utf-8'))
#Error handling
# url = "http://httpbin.org/html/thgb"
url = "http://no-such-server.org"
try:
response = urllib.request.urlopen(url)
if response.getcode()==HTTPStatus.OK:
print(response.read().decode('utf-8'))
except HTTPError as err:
print("Error: {0}".format(err.code))
except URLError as err:
print("that sucks {0}".format(err.reason))
import requests
from requests import HTTPError, Timeout
from requests.auth import HTTPBasicAuth
# url = "http://httpbin.org/xml"
url = "http://httpbin.org/get"
dataValues = {
"key1": "value1",
"key2": "value2"}
# response = requests.get(url, params=dataValues)
# print("status :" , response.status_code)
# print("headers :", response.headers)
# # print("contents :", response.content)
# print("contents as text :", response.text)
# post
url = "http://httpbin.org/delay/5"
# response = requests.post(url, data=dataValues)
# print("status :" , response.status_code)
# print("contents as text :", response.text)
# Custom header to the server
headerValues = {"User-Agent": "Gbenga App / 1.0.0"}
# error handling
# try:
# response = requests.get(url, headers=headerValues, timeout=2)
# response.raise_for_status()
# print("status :" , response.status_code)
# print("contents as text :", response.text)
# except HTTPError as err:
# print("Error: {0}".format(err))
# except Timeout as err:
# print("Request timed out: {0}".format(err))
url = "http://httpbin.org/basic-auth/gbenga/secret"
# myCreds = HTTPBasicAuth("GbengaAwodokun", "MySecretWord")
response = requests.get(url, auth=("gbenga", "secret"))
print(response.status_code)
print(response.text)
#requests
import requests
from requests import HTTPError, Timeout
from requests.auth import HTTPBasicAuth
# url = "http://httpbin.org/xml"
url = "http://httpbin.org/get"
dataValues = {
"key1": "value1",
"key2": "value2"}
# response = requests.get(url, params=dataValues)
# print("status :" , response.status_code)
# print("headers :", response.headers)
# # print("contents :", response.content)
# print("contents as text :", response.text)
# post
url = "http://httpbin.org/delay/5"
# response = requests.post(url, data=dataValues)
# print("status :" , response.status_code)
# print("contents as text :", response.text)
# Custom header to the server
headerValues = {"User-Agent": "Gbenga App / 1.0.0"}
# error handling
# try:
# response = requests.get(url, headers=headerValues, timeout=2)
# response.raise_for_status()
# print("status :" , response.status_code)
# print("contents as text :", response.text)
# except HTTPError as err:
# print("Error: {0}".format(err))
# except Timeout as err:
# print("Request timed out: {0}".format(err))
url = "http://httpbin.org/basic-auth/gbenga/secret"
# myCreds = HTTPBasicAuth("GbengaAwodokun", "MySecretWord")
response = requests.get(url, auth=("gbenga", "secret"))
print(response.status_code)
print(response.text)