V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
w9ay
V2EX  ›  Python

[ Python ] 为什么 requests hook 失败了

  •  
  •   w9ay · Jul 30, 2019 · 2604 views
    This topic created in 2465 days ago, the information mentioned may be changed or developed.

    requests 默认会对 url 的参数进行转义,我看了一下源码,问题出现在 requests.utils 的 requote_uri 中,所以我尝试 hook 这个函数。

    from urllib.parse import quote
    
    import requests
    
    
    def hook2(uri):
        print(11111111)
        safe_with_percent = "!#$%&'()*+,/:;=?@[]~{}^"
        return quote(uri, safe=safe_with_percent)
    
    
    requests.utils.requote_uri = hook2
    requests.get("http://127.0.0.1:8000", params='a=111&b=222!@$%^&({}')
    

    主要是对{}符号的转义,我想知道为什么这样不行呢?

    4 replies    2020-07-30 22:48:00 +08:00
    keakon
        1
    keakon  
       Jul 30, 2019   ❤️ 1
    models.py 里执行了 from .utils import requote_uri,你再去修改 requests.utils.requote_uri,不会对 models.py 里的 requote_uri 有任何影响。
    w9ay
        2
    w9ay  
    OP
       Jul 31, 2019
    @keakon 感谢回复,那有什么方法来 hook 它吗?
    keakon
        3
    keakon  
       Jul 31, 2019
    一个是在 import model 之前修改,另一个是修改 model.requote_uri
    lisongee
        4
    lisongee  
       Jul 30, 2020
    @keakon import 之前怎么修改?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3222 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 11:56 · PVG 19:56 · LAX 04:56 · JFK 07:56
    ♥ Do have faith in what you're doing.