-
Notifications
You must be signed in to change notification settings - Fork 10
/
urlutil.py
50 lines (32 loc) · 1 KB
/
urlutil.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
47
# -*- coding=utf-8 -*-
import urlparse
def reverseUrl(url):
"""
examples:
http://bar.foo.com:8983/to/index.html?a=b
com.foo.bar:http:8983/to/index.html?a=b
https://www.google.com.hk:8080/home/search;12432?newwi.1.9.serpuc#1234
hk.com.google.www:https:8080/home/search;12432?newwi.1.9.serpuc#1234
"""
reverse_url = ''
url = urlparse.urlsplit(url)
reverse_host = '.'.join(url.hostname.split('.')[::-1])
reverse_url += reverse_host
reverse_url += ':'
reverse_url += url.scheme
if url.port:
reverse_url += ':'
reverse_url += str(url.port)
if url.path:
reverse_url += url.path
if url.query:
reverse_url += '?'
reverse_url += url.query
if url.fragment:
reverse_url += '#'
reverse_url += url.fragment
return reverse_url
if __name__ == '__main__':
url = 'http://www.tianyancha.com/company/2313776032'
url = 'http://www.11467.com/foshan/co/444200.htm'
print reverseUrl(url)