urllib_parse_urlparse.py
from urllib.parse import urlparse
url = ' http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=1&network=1 '
parsed = urlparse(url)
print(parsed)
python3 urllib_parse_urlparse.py
ParseResult(scheme='http', netloc='test.dis.e.sogou', path='/adlist', params='', query='offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=5&model=2&terminal=3&network=1', fragment='')
urllib_parse_geturl.py
from urllib.parse import urlparse
original = ' http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=5&model=2&terminal=3&network=1 '
print('ORIG :', original)
parsed = parse.urlparse(original)
print('PARSED:', parsed.geturl())
$ python3 urllib_parse_geturl.py
ORIG : http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=5&model=2&terminal=3&network=1
PARSED: http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=5&model=2&terminal=3&network=1
urllib_parse_urljoin.py
from urllib.parse import urljoin
print(urljoin(' http://www.example.com/path/file.html ',
'anotherfile.html'))
print(urljoin(' http://www.example.com/path/file.html ',
'../ anotherfile.html'))
$ python3 urllib_parse_urljoin.py
http://www.example.com/path/anotherfile.html
http://www.example.com/anotherfile.html
urllib_parse_urljoin_with_path.py
print(urljoin(' http://www.example.com/path/ ',
'/subpath/file.html'))
print(urljoin(' http://www.example.com/path/ ',
'subpath/file.html'))
$ python3 urllib_parse_urljoin_with_path.py
http://www.example.com/subpath/file.html
http://www.example.com/path/subpath/file.html
urllib_parse_urlencode.py
from urllib.parse import urlencode
query_args = {
'q': 'query string',
'foo': 'bar',
}
encoded_args = urlencode(query_args)
print('Encoded:', encoded_args)
$ python3 urllib_parse_urlencode.py
Encoded: q=query+string&foo=bar
$ python3 urllib_parse_parse_qs.py
parse_qs : {'foo': ['foo1', 'foo2']}
parse_qsl: [('foo', 'foo1'), ('foo', 'foo2')]
test_dippatcher_adlist.py
url= ' http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=3&model=ios&terminal=1&version=2&network=1 ' http: //test.dis.e.sogou/adlist? offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=3&model=ios&terminal=1&version=2&network=1'
#Get request_id and auth
request_id = generate_requestId( expect [ 'platformId' ], expect [ 'posId' ])
auth = generate_auth(request_id, expect [ 'token' ])
#Modify the parameters in Url and replace request_id and auth
#Analysis URL
url_parsed = parse.urlparse(url)
bits = list(url_parsed)
qs = parse.parse_qs(bits[ four ])
#Replace interface input parameters in qs
qs[ 'requestId' ] = request_id
qs[ 'auth' ] = auth
qs[ 'offset' ] = expect [ 'offset' ]
qs[ 'count' ] = expect [ 'count' ]
qs[ 'model' ] = expect [ 'model' ]
qs[ 'terminal' ] = expect [ 'terminal' ]
qs[ 'version' ] = expect [ 'version' ]
qs[ 'network' ] = expect [ 'network' ]
#Edit Query Parameters
bits[ four ] = parse.urlencode(qs)
#URL reverse resolution
url_new = parse.urlunparse(bits)
print(url_new)
$ python3 test_dispatcher_adlist.py
bits: [ 'http' , 'test.dis.e.sogou' , '/adlist' , '' , "offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=3&model=ios&terminal=1&version=2&network=1' http://test.dis.e.sogou/adlist?offset=0&auth=69CF80EA062863279B72612FA5443B6F&requestId=0025500016111592878436805&count=3&model=ios&terminal=1&version=2&network=1 " , '' ]
qs: { 'offset' : [ '0' ], 'auth' : [ '69CF80EA062863279B72612FA5443B6F' , '69CF80EA062863279B72612FA5443B6F' ], 'requestId' : [ '0025500016111592878436805' , '0025500016111592878436805' ], 'count' : [ '3' , '3' ], 'model' : [ 'ios' , 'ios' ], 'terminal' : [ '1' , '1' ], 'version' : [ '2' , '2' ], 'network' : [ "1' http://test.dis.e.sogou/adlist?offset=0 " , '1' ]}
bits[ four ]: offset= zero &auth= eight thousand two hundred and fifteen f55af287a62a29efe7a70fd3ba0d&requestId= 0025500016111593405114583 &count= one &model=eee&terminal= one &version=eee&network= one
http: //test.dis.e.sogou/adlist? offset=0&auth=8215f55af287a62a29efe7a70fd3ba0d&requestId=0025500016111593405114583&count=1&model=eee&terminal=1&version=eee&network=1