1
+ # Requests is a Python HTTP library, released under the Apache License 2.0. The goal of the project is to make HTTP requests
2
+ # simpler and more human-friendly. It is one of the most popular python libraries that is not included with python by default.
3
+
4
+ import requests
5
+
6
+
7
+ # Making A GET Request
8
+ def printResponse (resp ):
9
+ print (f'Response Code: +----- { resp .status_code } -----+' )
10
+ print ('\n ' )
11
+
12
+ print ('Headers: +----------------------+' )
13
+ print (resp .headers )
14
+ print ('\n ' )
15
+
16
+ print ('Returned data: +----------------------+' )
17
+ print (resp .text )
18
+
19
+
20
+ # Use requests to issue an HTTP GET request
21
+ url = 'http://httpbin.org/xml'
22
+ resp = requests .get (url )
23
+ printResponse (resp )
24
+
25
+
26
+ # Response Code: +----- 200 -----+
27
+ #
28
+ #
29
+ # Headers: +----------------------+
30
+ # {'Date': 'Wed, 11 Mar 2020 18:03:20 GMT', 'Content-Type': 'application/xml', 'Content-Length': '522', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
31
+ #
32
+ #
33
+ # Returned data: +----------------------+
34
+ # <?xml version='1.0' encoding='us-ascii'?>
35
+ #
36
+ # <!-- A SAMPLE set of slides -->
37
+ #
38
+ # <slideshow
39
+ # title="Sample Slide Show"
40
+ # date="Date of publication"
41
+ # author="Yours Truly"
42
+ # >
43
+ #
44
+ # <!-- TITLE SLIDE -->
45
+ # <slide type="all">
46
+ # <title>Wake up to WonderWidgets!</title>
47
+ # </slide>
48
+ #
49
+ # <!-- OVERVIEW -->
50
+ # <slide type="all">
51
+ # <title>Overview</title>
52
+ # <item>Why <em>WonderWidgets</em> are great</item>
53
+ # <item/>
54
+ # <item>Who <em>buys</em> WonderWidgets</item>
55
+ # </slide>
56
+ #
57
+ # </slideshow>
58
+ #
59
+ # Process finished with exit code 0
60
+
61
+ # Including Parameters
62
+ # Send some parameters to the URL via a GET request
63
+ # Requests handles this for you, no manual encoding
64
+ payload = {'Size' : 'Large' , 'Cream' : True , 'Sugar' : False }
65
+ url = 'http://httpbin.org/get'
66
+ resp = requests .get (url , params = payload )
67
+ printResponse (resp )
68
+ # Response Code: +----- 200 -----+
69
+ #
70
+ #
71
+ # Headers: +----------------------+
72
+ # {'Date': 'Wed, 11 Mar 2020 18:13:37 GMT', 'Content-Type': 'application/json', 'Content-Length': '410', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
73
+ #
74
+ #
75
+ # Returned data: +----------------------+
76
+ # {
77
+ # "args": {
78
+ # "Cream": "True",
79
+ # "Size": "Large",
80
+ # "Sugar": "False"
81
+ # },
82
+ # "headers": {
83
+ # "Accept": "*/*",
84
+ # "Accept-Encoding": "gzip, deflate",
85
+ # "Host": "httpbin.org",
86
+ # "User-Agent": "python-requests/2.23.0",
87
+ # "X-Amzn-Trace-Id": "Root=1-5e692a51-71b500ab1d13d674526bc5d0"
88
+ # },
89
+ # "origin": "192.168.10.1",
90
+ # "url": "http://httpbin.org/get?Size=Large&Cream=True&Sugar=False"
91
+ # }
92
+ #
93
+ #
94
+ # Process finished with exit code 0
95
+
96
+
97
+ # Making A POST Request
98
+ # Send some parameters to the URL via a GET request
99
+ # Requests handles this for you, no manual encoding
100
+ payload = {'Size' : 'Large' , 'Cream' : True , 'Sugar' : False }
101
+ url = 'http://httpbin.org/post'
102
+ resp = requests .post (url , data = payload )
103
+ printResponse (resp )
104
+ # Response Code: +----- 200 -----+
105
+ #
106
+ #
107
+ # Headers: +----------------------+
108
+ # {'Date': 'Wed, 11 Mar 2020 20:23:51 GMT', 'Content-Type': 'application/json', 'Content-Length': '526', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
109
+ #
110
+ #
111
+ # Returned data: +----------------------+
112
+ # {
113
+ # "args": {},
114
+ # "data": "",
115
+ # "files": {},
116
+ # "form": {
117
+ # "Cream": "True",
118
+ # "Size": "Large",
119
+ # "Sugar": "False"
120
+ # },
121
+ # "headers": {
122
+ # "Accept": "*/*",
123
+ # "Accept-Encoding": "gzip, deflate",
124
+ # "Content-Length": "33",
125
+ # "Content-Type": "application/x-www-form-urlencoded",
126
+ # "Host": "httpbin.org",
127
+ # "User-Agent": "python-requests/2.23.0",
128
+ # "X-Amzn-Trace-Id": "Root=1-5e6948d7-4b5b42c85acf7660e4e2c1a8"
129
+ # },
130
+ # "json": null,
131
+ # "origin": "10.10.10.10",
132
+ # "url": "http://httpbin.org/post"
133
+ # }
134
+ #
135
+ #
136
+ # Process finished with exit code 0
137
+
138
+
139
+ # Sending Custom Headers
140
+ # Pass a custom header to the server
141
+ url = "http://httpbin.org/get"
142
+ customHeader = {'User-Agent' : 'Gardens-Delight-App / 1.0.1' }
143
+ resp = requests .get (url , headers = customHeader )
144
+ printResponse (resp )
145
+ # Response Code: +----- 200 -----+
146
+ #
147
+ #
148
+ # Headers: +----------------------+
149
+ # {'Date': 'Wed, 11 Mar 2020 20:46:31 GMT', 'Content-Type': 'application/json', 'Content-Length': '312', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
150
+ #
151
+ #
152
+ # Returned data: +----------------------+
153
+ # {
154
+ # "args": {},
155
+ # "headers": {
156
+ # "Accept": "*/*",
157
+ # "Accept-Encoding": "gzip, deflate",
158
+ # "Host": "httpbin.org",
159
+ # "User-Agent": "Gardens-Delight-App / 1.0.1",
160
+ # "X-Amzn-Trace-Id": "Root=1-5e694e27-6ade43401b07635c60af1748"
161
+ # },
162
+ # "origin": "1.2.3.4",
163
+ # "url": "http://httpbin.org/get"
164
+ # }
165
+
166
+ # Handling Errors With HTTPError
167
+ from requests .exceptions import HTTPError , Timeout
168
+
169
+ try :
170
+ url = 'http://httpbin.org/status/404'
171
+ resp = requests .get (url )
172
+ resp .raise_for_status ()
173
+ printResponse (resp )
174
+ except HTTPError as error :
175
+ print (f'Http Error: { error } ' )
176
+ except Timeout as error :
177
+ print (f'Request timed out: { error } ' )
178
+ # Http Error: 404 Client Error: NOT FOUND for url: http://httpbin.org/status/404
179
+
180
+
181
+ # Handling A Timeout
182
+ try :
183
+ url = 'http://httpbin.org/delay/5'
184
+ resp = requests .get (url , timeout = 3 )
185
+ resp .raise_for_status ()
186
+ printResponse (resp )
187
+ except HTTPError as error :
188
+ print (f'Http Error: { error } ' )
189
+ except Timeout as error :
190
+ print (f'Request timed out: { error } ' )
191
+ # Request timed out: HTTPConnectionPool(host='httpbin.org', port=80): Read timed out. (read timeout=3)
192
+
193
+ # Authentication With Requests
194
+ from requests .auth import HTTPBasicAuth
195
+
196
+ # Access a URL that requires authentication - the format of this
197
+ # URL is that you provide the username/password to auth against
198
+ url = 'https://httpbin.org/basic-auth/vegibit/secret'
199
+
200
+ # Create a credentials object using HTTPBasicAuth
201
+ credentials = HTTPBasicAuth ('vegibit' , 'secret' )
202
+
203
+ # Issue the request with the authentication credentials
204
+ resp = requests .get (url , auth = credentials )
205
+ printResponse (resp )
206
+ # Response Code: +----- 200 -----+
207
+ #
208
+ #
209
+ # Headers: +----------------------+
210
+ # {'Date': 'Thu, 12 Mar 2020 14:36:41 GMT', 'Content-Type': 'application/json', 'Content-Length': '50', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
211
+ #
212
+ #
213
+ # Returned data: +----------------------+
214
+ # {
215
+ # "authenticated": true,
216
+ # "user": "vegibit"
217
+ # }
0 commit comments