-
Notifications
You must be signed in to change notification settings - Fork 0
/
urllibtest.py
43 lines (35 loc) · 1.11 KB
/
urllibtest.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
#!/usr/bin/env python
import urllib
# url handler respone
base = r'http://localhost:8081'
data3 = urllib.urlencode({"time":1323333"user":"ygs","action":"answer","content":"abcde"})
# data3 = { 'name': 'dark-bull', 'age': 200 }
handler = urllib.urlopen(base, data3)
print r'http header:/n', handler.info()
print r'http status:', handler.getcode()
print r'url:', handler.geturl()
def cbk(a, b, c):
# a : has download counts
# b : block size
# c : remote file size
per = 100.0 * a * b / c
if per > 100:
per = 100
print '%.2f%%' % per
# download url
href = r'http://down.51voa.com/201303/us-budget-impasse-could-affect-air-travel.mp3'
local = r'./test.mp3'
urllib.urlretrieve(href, local, cbk)
handler.close()
# url escape
data = 'name=dasf'
data1 = urllib.quote(data)
print data1
print urllib.unquote(data1)
print urllib.quote_plus(data)
# json file
data3 = urllib.urlencode({ 'name': 'dark-bull', 'age': 200 })
print data3
data4 = urllib.pathname2url(r'd:/a/b/c/23.php')
print data4 # result: ///D|/a/b/c/23.php
print urllib.url2pathname(data4) # result: D:/a/b/c/23.php