Why is there such a difference between the URL encoding behavior of requests and urllib,urllib2,urllib3? __ Code

Source: Internet
Author: User
Tags html form urlencode

#!/usr/bin/env python
#coding: Utf-8
Import Requests,urllib,urllib2,urllib3,urlparse

url = "Http://xxx.com/index.php?Q=u=%OS%26%20"
Print "Original:", url
#print requests.get (URL). Content
Print "-------------------"
url_parts = urlparse.urlparse (URL)
Print "splited:", Url_parts.query
Print "-------------------"
params = Dict (URLPARSE.PARSE_QSL (url_parts.query,true))
Print "parsed:", "q=" +params[' Q ']
Print "-------------------"
url_dealed = Urlparse.urlunsplit (url_parts.scheme,url_parts.netloc\
, Url_parts.path,urllib.urlencode (params) \
, url_parts.fragment))
Print "unsplited (dealed_final):", url_dealed
Print "-------------------"

#requests. Get (url= "http://xxx.com/index.php?Q=u=%OS%26%20"). Content
#requests. Get (url= "http://xxx.com/index.php", params). Content
Print "Requests:", "http://xxx.com/index.php?Q=u=%25OS%2526%2520"

#urllib. Urlopen (url= "http://xxx.com/index.php?Q=u=%OS%26%20"). Read ()
Print "Urllib:", "http://xxx.com/index.php?Q=u=%OS%26%20"

#urllib2. Urlopen (url= "http://xxx.com/index.php?Q=u=%OS%26%20"). Read ()
Print "URLLIB2:", "http://xxx.com/index.php?Q=u=%OS%26%20"

#urllib3. Proxymanager (' http://localhost:8888/'). Request ("Get", "http://xxx.com/index.php?Q=u%3D%25OS%26+"). Data
Print "URLLIB3:", "http://xxx.com/index.php?Q=u=%OS%26%20"

#url_dealed = "http://xxx.com/index.php?Q=u%3D%25OS%26+"
#requests. Get (url_dealed). Content
#urllib3. Proxymanager (' http://localhost:8888/'). Request ("Get", url_dealed). Data
#urllib2. Urlopen (url_dealed). Read ()
#urllib. Urlopen (url_dealed). Read ()
Print "All requests dealed_final:", "http://xxx.com/index.php?Q=u%3D%25OS%26+"
Borrow the number to send a learning exchange posts, to find the way great God Popular Science four python library default encoding behavior differences:
Today, test injection, the browser URL posted to the requests parameters in the contract has not been expected to respond to the decision to grab a bag diff, did not think really requests did the hands and feet.
1. Requests request http://xxx.com/index.php?Q=u=%25OS%2526%2520 will encode the% of Q value as%25.
2. Requests request after the split-code-assembly processing http://xxx.com/index.php?Q=u%3D%25OS%26+ remain intact.
3. URLLIB,URLLIB2,URLLIB3 the URL request in 1,2 is intact.
The good requests is the encapsulated URLLIB3, why change this default behavior. Collection Thanks (0) share to: 0 10 reply Answer | 2016-03-03 20:55

2# Invisible Man really busy (focus on security research and development and vulnerabilities) | 2016-03-03 20:59

This is a pit requests code is very powerful in general my words with the Urllib this method:
Urllib.quote (string[, safe])
Encodes a string. Parameter safe specifies characters that do not need to be encoded. As for why can only see the source code slightly ... 3# Null_z | 2016-03-03 21:18

If you're stitching up your own URL and then use requests to not encode
If the dictionary is passed in as a parameter, the requests is encoded once
Requests is not a standard library. 4# Larry (How to sign the end MT i) | 2016-03-03 21:28

With the second floor, today also want to UrlEncode, feel can also use Urllib.quote_plus (string[, safe) can be encoded in the URL HTML form of the query string, and then stitching their own 5# fate0 (I will wait for you in the future) | 2016-03-03 22:58

https://github.com/kennethreitz/requests/blob/master/requests%2Futils.py#L443

Because the OS is not a legitimate 16-in-6#, a small slag | 2016-03-04 08:37

I have also been in the tangle of writing exp sometimes use a similar%u0027 to circumvent some restrictions, but with this coded URL directly error 7# zxx | 2016-03-04 11:16

From requests import Request, session
s = Session ()
Url= "Http://xxx.com/index.php?Q=u=%OS%26%20"
req = Request (' Get ', url)
prepped = S.prepare_request (req)
Prepped.url = Prepped.url.replace ('%25 ', '% ')
RESP = s.send (prepped) 8# Modnar | 2016-03-04 11:46

@fate0 # We couldn ' t unquote the given URI, so let ' s try quoting it, but
# There May is unquoted '% ' s in the URI. We need to make sure they ' re
# properly quoted so they does not cause issues elsewhere
In order to not decode the 16, so simply rough coding%, but also affect the other can decode the 16, a bit incomprehensible ... Traditional browsers have not done so, and requests does not offer a switch. 9# Modnar | 2016-03-04 11:49

@ The invisible man is really busy. This can be addressed by requests to the Urllib.urlencode (Dict (URLPARSE.PARSE_QSL (urlparse.urlparse (URL). query,true)), Decoding is also encoded after the%26 and%20 reserved ~ 10# Invisible Man really busy (focus on security research and development and vulnerabilities) | 2016-03-04 17:20

@ModNar Indeed    was careful to read the documentation for   , but there was no such flag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.