• Requests 方法 -- post请求操作实践


    1、登录Jenkins抓包 ,小编的Jenkins部署在Tomcat上,把Jenkins.war 包放置到webapps目录。

    本次用浏览器自带抓包,按下F12操作,主要看post就可以,登录是向服务器提交表单操作,则为post请求如下:

    2、body里面并不是json格式的,而是application/x-www-form-urlencoded,部分请求截图如下:

    3、实现登录代母如下:

    import requests
    # 先打开登录首页,获取部分 cookie
    url1 = "http://localhost:8081/jenkins/j_acegi_security_check"
    headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "screenResolution=1366x768; JSESSIONID=7B7F3F66948B1965ADDD332E5425A7F4; Pycharm-470d2d8b=7eb39a7e-c2b5-41ac-95f2-ee8fdf8f2188; jenkins-timestamper-offset=-28800000; csrftoken=qsBdlvEy1RoGOFiUqpvowtF9bkeP8XAXbZAhrZnOBqgwqmX3AA6TCxHFkAPSqBsN; hudson_auto_refresh=true; _ga=GA1.1.1092626020.1560260048; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTU2Mjc2NjkzMTQzODo1ODAxZTNhNDExMDlkODU1ZjFkZWIzZDg1ZGQxNWY4ZjA3NDExMmY2MDJlZDg4YzhlYmQ4YWZhYzMxYTc2OWY3; Pycharm-b245d0ed=4a1ebe68-e0eb-4b41-b28d-d0edee006e77",
    "Origin": "http://localhost:8081",
    "Referer": "http://localhost:8081/jenkins/login?from=%2Fjenkins%2F",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
    }

    form_data = {
    "j_username":" 自己的账号",
    "j_password": "自己的密码",
    "from": "/jenkins/",
    "Submit": "登录",
    "remember_me": "on"
    }

    r = requests.post(url=url1,headers=headers,data=form_data,verify=False)
    # session 会话
    # s = requests.session()
    print(r.text)

    4、返回的结果并不是json的格式,而是HTML格式,这里只截图部分的html结果

    5、也可以用正则表达式进行提取是否登录成功,这里截取HTML的两个关键字 ,代码如下:

    import requests
    # 先打开登录首页,获取部分 cookie
    url1 = "http://localhost:8081/jenkins/j_acegi_security_check"
    headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "screenResolution=1366x768; JSESSIONID=7B7F3F66948B1965ADDD332E5425A7F4; Pycharm-470d2d8b=7eb39a7e-c2b5-41ac-95f2-ee8fdf8f2188; jenkins-timestamper-offset=-28800000; csrftoken=qsBdlvEy1RoGOFiUqpvowtF9bkeP8XAXbZAhrZnOBqgwqmX3AA6TCxHFkAPSqBsN; hudson_auto_refresh=true; _ga=GA1.1.1092626020.1560260048; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTU2Mjc2NjkzMTQzODo1ODAxZTNhNDExMDlkODU1ZjFkZWIzZDg1ZGQxNWY4ZjA3NDExMmY2MDJlZDg4YzhlYmQ4YWZhYzMxYTc2OWY3; Pycharm-b245d0ed=4a1ebe68-e0eb-4b41-b28d-d0edee006e77",
    "Origin": "http://localhost:8081",
    "Referer": "http://localhost:8081/jenkins/login?from=%2Fjenkins%2F",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
    }

    form_data = {
    "j_username":" 自己的账号",
    "j_password": "自己的密码",
    "from": "/jenkins/",
    "Submit": "登录",
    "remember_me": "on"
    }

    r = requests.post(url=url1,headers=headers,data=form_data,verify=False)
    # session 会话
    # s = requests.session()
    print(r.text)

    # 正则表达式提取账号和登录按钮
    import re
    t = re.findall("<b>(.+?)</b>",r.content.decode("utf-8")) #由于是编码格式,所以要进行decode解码
    print(t[0])
    print(t[1])

    运行的结果为 1.自己的账号名 2.注销 。

    6、复制出html页面到 http://www.kjson.com/xml/ 中进行解析下,有层次感。

    
    
  • 相关阅读:
    转--Android中调用webservice的工具类
    转--Android实现ListView过滤功能,继承于BaseAdapter,非ArrayAdapter。
    Kubernetes 1.5 配置dns
    Kubernetes 1.5安装
    HAproxy健康检查的三种方式
    某电商网站线上drbd+heartbeat+nfs配置
    sonarqube代码检测
    Sersync实时同步
    RabbitMQ配置文件
    SVNManager配置
  • 原文地址:https://www.cnblogs.com/Teachertao/p/11143431.html
Copyright © 2020-2023  润新知