推荐学习书目
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
fnmsd
V2EX  ›  Python

求问 Python 如何进行全部字符的 URL 编码?

  •  
  •   fnmsd · Apr 5, 2017 · 3603 views
    This topic created in 3329 days ago, the information mentioned may be changed or developed.
    比如 admin 在用 urllib 的 quote 时还是 admin ,希望能够对非特殊字符也进行 URL 编码,变为%61%64%6D%69%6E

    Google 的好半天没有查到,求高手解答!
    11 replies    2017-04-06 17:24:49 +08:00
    Kisesy
        1
    Kisesy  
       Apr 5, 2017
    你抄一下官方库代码, 自己写个函数把这个限制去掉不就好了, 就几句代码
    ldbC5uTBj11yaeh5
        2
    ldbC5uTBj11yaeh5  
       Apr 5, 2017   ❤️ 1
    re.sub(r'.', lambda m: '%%%s' % m.group(0).encode('hex').upper(), 'admin')
    Kisesy
        3
    Kisesy  
       Apr 5, 2017
    a = b'admin'
    b = ''.join('%{:02X}'.format(x) for x in a)
    print(b)
    Luckyray
        4
    Luckyray  
       Apr 5, 2017
    对英文字符也编码的好处或者用处是啥?
    lrxiao
        5
    lrxiao  
       Apr 5, 2017
    urllib2.quote
    glasslion
        6
    glasslion  
       Apr 5, 2017
    admin ? 你该不会是想通过 url encode 的方式 来搞 sql 防注入之类的 东东吧? 慎之。
    fnmsd
        7
    fnmsd  
    OP
       Apr 6, 2017
    @Kisesy 感谢~懒得自己改了,想看看有没有直接能用的,我在 python2.7 中试 format 里的 x 还需要加 ord~
    fnmsd
        8
    fnmsd  
    OP
       Apr 6, 2017
    @jigloo 学习了~re 的 sub 还可以这么用
    fnmsd
        9
    fnmsd  
    OP
       Apr 6, 2017
    @jigloo 感谢~
    fnmsd
        10
    fnmsd  
    OP
       Apr 6, 2017
    @lrxiao 是完全 URL 编码, quote 只编码影响 URL 的特殊字符
    fnmsd
        11
    fnmsd  
    OP
       Apr 6, 2017
    @Luckyray 测试用途
    @glasslion 当然不是 绕过注入防护还差不多~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4113 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 05:10 · PVG 13:10 · LAX 22:10 · JFK 01:10
    ♥ Do have faith in what you're doing.