-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpython-urllib.html
More file actions
353 lines (315 loc) · 16.9 KB
/
python-urllib.html
File metadata and controls
353 lines (315 loc) · 16.9 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="images/logo.png">
<link rel="apple-touch-icon" href="images/logo.png">
<title>Python urllib - Developer Toolkit | DevDunia</title>
<meta name="description" content="Python urllib HTTP client library examples and code snippets. Free online Python urllib reference for developers.">
<script src="https://cdn.tailwindcss.com"></script>
<script src="js/common.js"></script>
<style>
body {
background: linear-gradient(135deg, #0f172a 0%, #1e293b 50%, #0f172a 100%);
}
</style>
</head>
<body class="h-screen lg:ml-72">
<!-- Background: Consistent Subtle Dark Gradient -->
<div class="fixed inset-0 -z-10 bg-gradient-to-br from-gray-900 via-slate-900 to-gray-900"></div>
<!-- Main Container -->
<div class="main-content">
<div class="relative z-10 container mx-auto px-4 pt-16 pb-24 sm:px-6 lg:px-8">
<!-- Page Header -->
<div class="text-center mb-12">
<h1 class="text-3xl sm:text-4xl font-bold mb-4 tracking-tight
text-transparent bg-clip-text bg-gradient-to-r from-emerald-400 to-teal-500">
Python urllib
</h1>
<p class="text-lg text-gray-400 max-w-2xl mx-auto">
Built-in HTTP client library with request/response handling examples.
</p>
</div>
<!-- Tool Container -->
<div class="max-w-6xl mx-auto">
<div class="bg-slate-800/70 backdrop-blur-md rounded-lg shadow-lg border border-slate-700/60 p-6">
<!-- HTTP Methods Section -->
<div class="mb-8">
<h2 class="text-xl font-semibold text-emerald-300 mb-4">HTTP Methods</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- GET Request -->
<div class="bg-slate-700/30 rounded-lg p-4 border border-slate-600/50">
<div class="flex items-center justify-between mb-3">
<h3 class="text-lg font-medium text-emerald-300">GET Request</h3>
<button class="copy-btn px-3 py-1 bg-emerald-600 hover:bg-emerald-700 text-white text-xs rounded transition-colors duration-200 flex items-center space-x-1" data-target="get-code">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v8a2 2 0 01-2 2h-2m-6-4l2 2m0 0l2-2m-2 2V10"></path>
</svg>
<span>Copy</span>
</button>
</div>
<textarea
id="get-code"
rows="8"
class="w-full px-3 py-2 bg-slate-800/50 border border-slate-600 rounded text-gray-200 font-mono text-sm resize-none"
readonly
>import urllib.request
import urllib.parse
import json
# Simple GET request
url = "https://api.github.com/users/octocat"
response = urllib.request.urlopen(url)
data = response.read()
print(data.decode('utf-8'))
# GET with parameters
params = {'q': 'python', 'sort': 'stars'}
url = "https://api.github.com/search/repositories?" + urllib.parse.urlencode(params)
response = urllib.request.urlopen(url)
data = json.loads(response.read().decode('utf-8'))
print(data)</textarea>
</div>
<!-- POST Request -->
<div class="bg-slate-700/30 rounded-lg p-4 border border-slate-600/50">
<div class="flex items-center justify-between mb-3">
<h3 class="text-lg font-medium text-emerald-300">POST Request</h3>
<button class="copy-btn px-3 py-1 bg-emerald-600 hover:bg-emerald-700 text-white text-xs rounded transition-colors duration-200 flex items-center space-x-1" data-target="post-code">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v8a2 2 0 01-2 2h-2m-6-4l2 2m0 0l2-2m-2 2V10"></path>
</svg>
<span>Copy</span>
</button>
</div>
<textarea
id="post-code"
rows="8"
class="w-full px-3 py-2 bg-slate-800/50 border border-slate-600 rounded text-gray-200 font-mono text-sm resize-none"
readonly
>import urllib.request
import urllib.parse
import json
# POST with JSON data
url = "https://httpbin.org/post"
data = {"name": "John", "age": 30}
json_data = json.dumps(data).encode('utf-8')
req = urllib.request.Request(url, data=json_data)
req.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(req)
result = response.read().decode('utf-8')
print(result)
# POST with form data
form_data = urllib.parse.urlencode({'username': 'john', 'password': 'secret'}).encode('utf-8')
req = urllib.request.Request(url, data=form_data)
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))</textarea>
</div>
<!-- PUT Request -->
<div class="bg-slate-700/30 rounded-lg p-4 border border-slate-600/50">
<div class="flex items-center justify-between mb-3">
<h3 class="text-lg font-medium text-emerald-300">PUT Request</h3>
<button class="copy-btn px-3 py-1 bg-emerald-600 hover:bg-emerald-700 text-white text-xs rounded transition-colors duration-200 flex items-center space-x-1" data-target="put-code">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v8a2 2 0 01-2 2h-2m-6-4l2 2m0 0l2-2m-2 2V10"></path>
</svg>
<span>Copy</span>
</button>
</div>
<textarea
id="put-code"
rows="8"
class="w-full px-3 py-2 bg-slate-800/50 border border-slate-600 rounded text-gray-200 font-mono text-sm resize-none"
readonly
>import urllib.request
import urllib.parse
import json
# PUT request to update resource
url = "https://httpbin.org/put"
data = {"id": 1, "name": "Updated Name", "status": "active"}
json_data = json.dumps(data).encode('utf-8')
req = urllib.request.Request(url, data=json_data, method='PUT')
req.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(req)
result = response.read().decode('utf-8')
print(result)
# PUT with custom headers
headers = {
'Authorization': 'Bearer your-token-here',
'Content-Type': 'application/json'
}
req = urllib.request.Request(url, data=json_data, method='PUT', headers=headers)
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))</textarea>
</div>
<!-- DELETE Request -->
<div class="bg-slate-700/30 rounded-lg p-4 border border-slate-600/50">
<div class="flex items-center justify-between mb-3">
<h3 class="text-lg font-medium text-emerald-300">DELETE Request</h3>
<button class="copy-btn px-3 py-1 bg-emerald-600 hover:bg-emerald-700 text-white text-xs rounded transition-colors duration-200 flex items-center space-x-1" data-target="delete-code">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v8a2 2 0 01-2 2h-2m-6-4l2 2m0 0l2-2m-2 2V10"></path>
</svg>
<span>Copy</span>
</button>
</div>
<textarea
id="delete-code"
rows="8"
class="w-full px-3 py-2 bg-slate-800/50 border border-slate-600 rounded text-gray-200 font-mono text-sm resize-none"
readonly
>import urllib.request
import urllib.parse
# DELETE request
url = "https://httpbin.org/delete"
req = urllib.request.Request(url, method='DELETE')
response = urllib.request.urlopen(req)
result = response.read().decode('utf-8')
print(result)
# DELETE with authentication
headers = {
'Authorization': 'Bearer your-token-here'
}
req = urllib.request.Request(url, method='DELETE', headers=headers)
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))
# DELETE with query parameters
params = {'id': '123'}
url_with_params = url + '?' + urllib.parse.urlencode(params)
req = urllib.request.Request(url_with_params, method='DELETE')
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))</textarea>
</div>
</div>
</div>
<!-- Advanced Features Section -->
<div class="mb-8">
<h2 class="text-xl font-semibold text-emerald-300 mb-4">Advanced Features</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- Error Handling -->
<div class="bg-slate-700/30 rounded-lg p-4 border border-slate-600/50">
<div class="flex items-center justify-between mb-3">
<h3 class="text-lg font-medium text-emerald-300">Error Handling</h3>
<button class="copy-btn px-3 py-1 bg-emerald-600 hover:bg-emerald-700 text-white text-xs rounded transition-colors duration-200 flex items-center space-x-1" data-target="error-code">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v8a2 2 0 01-2 2h-2m-6-4l2 2m0 0l2-2m-2 2V10"></path>
</svg>
<span>Copy</span>
</button>
</div>
<textarea
id="error-code"
rows="10"
class="w-full px-3 py-2 bg-slate-800/50 border border-slate-600 rounded text-gray-200 font-mono text-sm resize-none"
readonly
>import urllib.request
import urllib.error
def make_request(url):
try:
response = urllib.request.urlopen(url)
data = response.read().decode('utf-8')
return data
except urllib.error.HTTPError as e:
print(f"HTTP Error {e.code}: {e.reason}")
return None
except urllib.error.URLError as e:
print(f"URL Error: {e.reason}")
return None
except Exception as e:
print(f"Unexpected error: {e}")
return None
# Usage
url = "https://httpbin.org/status/404"
result = make_request(url)
if result:
print(result)
else:
print("Request failed")</textarea>
</div>
<!-- File Download -->
<div class="bg-slate-700/30 rounded-lg p-4 border border-slate-600/50">
<div class="flex items-center justify-between mb-3">
<h3 class="text-lg font-medium text-emerald-300">File Download</h3>
<button class="copy-btn px-3 py-1 bg-emerald-600 hover:bg-emerald-700 text-white text-xs rounded transition-colors duration-200 flex items-center space-x-1" data-target="download-code">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v8a2 2 0 01-2 2h-2m-6-4l2 2m0 0l2-2m-2 2V10"></path>
</svg>
<span>Copy</span>
</button>
</div>
<textarea
id="download-code"
rows="10"
class="w-full px-3 py-2 bg-slate-800/50 border border-slate-600 rounded text-gray-200 font-mono text-sm resize-none"
readonly
>import urllib.request
import os
def download_file(url, filename):
try:
# Download file
urllib.request.urlretrieve(url, filename)
print(f"File downloaded: {filename}")
# Get file info
file_size = os.path.getsize(filename)
print(f"File size: {file_size} bytes")
except Exception as e:
print(f"Download failed: {e}")
# Download a file
url = "https://httpbin.org/robots.txt"
filename = "robots.txt"
download_file(url, filename)
# Download with progress
def download_with_progress(url, filename):
def progress_hook(block_num, block_size, total_size):
downloaded = block_num * block_size
if total_size > 0:
percent = min(100, (downloaded * 100) // total_size)
print(f"\rDownloaded: {percent}%", end='')
urllib.request.urlretrieve(url, filename, progress_hook)
print(f"\nDownload complete: {filename}")
# download_with_progress(url, filename)</textarea>
</div>
</div>
</div>
<!-- Quick Reference Section -->
<div class="mt-8 p-4 bg-slate-700/30 rounded-lg border border-slate-600/50">
<h3 class="text-lg font-semibold text-emerald-400 mb-3">Quick Reference</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
<div>
<p class="text-gray-300 font-medium mb-2">Common Modules:</p>
<ul class="text-gray-400 space-y-1">
<li>• <code>urllib.request</code> - HTTP requests</li>
<li>• <code>urllib.parse</code> - URL parsing</li>
<li>• <code>urllib.error</code> - Exception handling</li>
<li>• <code>urllib.robotparser</code> - robots.txt parsing</li>
</ul>
</div>
<div>
<p class="text-gray-300 font-medium mb-2">Key Functions:</p>
<ul class="text-gray-400 space-y-1">
<li>• <code>urlopen()</code> - Open URLs</li>
<li>• <code>urlretrieve()</code> - Download files</li>
<li>• <code>urlencode()</code> - Encode parameters</li>
<li>• <code>Request()</code> - Create requests</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Sidebar Scripts -->
<!-- Include Final Sidebar -->
<div id="sidebar-container"></div>
<script>
// Load final_sidebar.html content
fetch('final_sidebar.html')
.then(response => response.text())
.then(html => {
document.getElementById('sidebar-container').innerHTML = html;
})
.catch(error => {
console.error('Error loading sidebar:', error);
});
</script>
</body>
</html>