-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtesturllib.py
46 lines (35 loc) · 1.14 KB
/
testurllib.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
44
45
46
try:
from urllib.parse import urlparse, urljoin
except ImportError:
from urlparse import urlparse, urljoin
import json
import sys
f = open('urltestdata.json')
tests = json.load(f)
f.close()
constructor_results = []
for test in tests:
result = {'input': test['input'], 'base': test['base']}
try:
url = urlparse(urljoin(test['base'], test['input']))
except:
result['exception'] = str(sys.exc_info()[1])
url = urlparse('')
result['href'] = url.geturl()
result['protocol'] = url.scheme + ':'
try:
result['port'] = url.port
except:
result['port_exception'] = str(sys.exc_info()[1])
result['username'] = url.username
result['password'] = url.password
result['hostname'] = url.hostname
result['pathname'] = url.path
result['search'] = '?' + url.query if url.query else ''
result['hash'] = '#' + url.fragment if url.fragment else ''
# http://bugs.python.org/issue23150
if url.params: result['pathname'] += ';' + url.params
constructor_results.append(result)
useragent = 'python ' + sys.version.replace(" \n", " ")
print(json.dumps({'useragent': useragent, 'constructor': constructor_results},
indent=2))