• Python-访问网络资源


    使用python写接口自动化脚本的时候,会使用到一系列请求来访问网络资源

    使用python内置模块

    from urllib import request
    
    
    def get_html(url):
        page = request.urlopen(url)
        html = page.read().decode('utf-8')   # 如果不用decode,获取的会是bytes
        return html

    使用python第三方模块

    import requests
    
    
    def get_status():
        resutls = requests.get("https://jt377.my.centrify.com/my?customerId=JT377")
        resutls_status_code = resutls.status_code
        print(resutls_status_code)   # 200
    
    
    def get_html():
        resutls = requests.get("https://jt377.my.centrify.com/my?customerId=JT377")
        resutls_html = resutls.text  # 通过文本的形式获取响应内容html
        resutls_html.encode(resutls.encoding).decode()  # 处理乱码
        print(resutls_html)    # <>
    
    
    def get_json():
        request_headers = {"X-CENTRIFY-NATIVE-CLIENT": "Web", "Content-Type": "application/json"}
        user_name_payload = {"AssociatedEntityName": "Portal", "AssociatedEntityType": "Portal",
                             "ExtIdpAuthChallengeState": "", "TenantId": "jt377", "User": "cat_test1@jt377",
                             "Version": "1.0"}
        resutls = requests.post("https://jt377.my.centrify.com/Security/StartAuthentication",
                             json=user_name_payload, headers=request_headers)
        resutls_json = resutls.json()  # 通过json形式获取响应内容
        print(resutls_json)   # {}
    
    
    def get_cookie():
        r = requests.Session()   # 自动获取cookie,并传入后面所有的请求中,经常用于web自动化让整个过程保持登录状态
        request_headers = {"X-CENTRIFY-NATIVE-CLIENT": "Web", "Content-Type": "application/json"}
        user_name_payload = {"AssociatedEntityName": "Portal", "AssociatedEntityType": "Portal",
                             "ExtIdpAuthChallengeState": "", "TenantId": "jt377", "User": "cat_test1@jt377",
                             "Version": "1.0"}
        resutls = r.post("https://jt377.my.centrify.com/Security/StartAuthentication",
                                json=user_name_payload, headers=request_headers)
        headers = r.headers
        print(headers)
  • 相关阅读:
    常量
    语音合成技术(深度学习方法简介)
    在 Linux 上如何清除内存的 Cache、Buffer 和交换空间
    Bash Bang (!) commands(bash的“!”命令,重新运行前一个命令的全部或部分。)
    Ubuntu apt pip conda 代理设置
    rsync 远程数据同步
    设置 jupyter notebook 可远程访问
    ubuntu 挂载硬盘(GPT分区,大于2T的硬盘)
    用树结构构造一篇文章(待完成)
    数组切分(句子拼接)
  • 原文地址:https://www.cnblogs.com/lj8023wh/p/10605329.html
Copyright © 2020-2023  润新知