V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
2joe
V2EX  ›  Python

登录请求构造,各位帮忙看下拉。

  •  
  •   2joe · 2019-06-25 12:33:47 +08:00 · 1303 次点击
    这是一个创建于 1982 天前的主题,其中的信息可能已经有所发展或是发生改变。
    浏览器请求登录如下
    POST http://127.0.0.1/userLogin.asp HTTP/1.1
    Host: 127.0.0.1
    Connection: Keep-Alive
    Content-Length: 79
    Pragma: no-cache
    Cache-Control: no-cache
    Accept: image/jpeg, image/gif, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*
    Accept-Language: zh-CN
    Content-Type: application/x-www-form-urlencoded
    User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)
    Referer: http://127.0.0.1/userLogin.asp
    Accept-Encoding: gzip, deflate
    save2Cookie=&vldcode=&account=admin&password=fgs@mm&btnSubmit=+%B5%C7%C2%BC+

    我 py 写成这样
    login_url = "http://127.0.0.1/userLogin.asp"
    admin_url = "http://127.0.0.1/menu_admin.asp"
    headers1 = {
    "Accept-Encoding":"gzip, deflate",
    "Host":"127.0.0.1",
    "Referer":"http://10.41.10.254/menu_admin.asp",
    "Upgrade-Insecure-Requests":"1",
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded",
    }
    data1 = {
    "save2Cookie":"","vldcode":"","account":"admin","password":"fgs@mm","btnSubmit":"登录"
    }
    data_gb2312 = urlencode(data1, encoding='gb2312')
    sesion = requests.session()
    sesion.post(url=login_url,headers=headers1,data=data_gb2312)
    aa=sesion.get(admin_url)
    print(aa.content.decode("gb2312"))

    我用 fd 看到我请求是这样的
    POST http://127.0.0.1/userLogin.asp HTTP/1.1
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36
    Accept-Encoding: gzip, deflate
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    Connection: keep-alive
    Accept-Language: zh-CN,zh;q=0.9
    Cache-Control: no-cache
    Host: 127.0.0.1
    Pragma: no-cache
    Connection: keep-alive
    Referer: http://127.0.0.1/home.asp
    Upgrade-Insecure-Requests: 1
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 79

    save2Cookie=&vldcode=&account=admin&password=fgs%40mm&btnSubmit=%B5%C7%C2%BC


    是我的构造有问题吗?还是接口不对,我发现%B5%C7%C2%BC 正常请求 是有个+ 号的,各位帮忙看看,一个路由登录
    3 条回复    2019-09-20 21:27:11 +08:00
    lc1450
        1
    lc1450  
       2019-06-25 13:17:14 +08:00
    直接 encode 登录 就行了, +是因为空格被转换的

    >>> from urllib.parse import urlencode
    >>> urlencode({'a': 324, "vv": ' dgdg '})
    'a=324&vv=+dgdg+'

    还要注意 password 的 @也被转换了, 像这种情况,直接把 btnSubmit 写成转换后的,就不用 urlencode 了
    EdgeDR
        2
    EdgeDR  
       2019-09-17 23:53:34 +08:00
    小白想在这里问一下
    为什么我的登录编码出来是这个呀?

    >>>from urllib.parse import quote,unquote
    >>>print(quote('登录'))
    %E7%99%BB%E5%BD%95

    btnLogon=%B5%C7%C2%BC
    我想构造这个,但是我对它解码是乱码。。。。
    EdgeDR
        3
    EdgeDR  
       2019-09-20 21:27:11 +08:00
    已经解决了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5448 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:34 · PVG 15:34 · LAX 23:34 · JFK 02:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.