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