zoukankan      html  css  js  c++  java
  • 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 ubuntu装机到可实现java(eclipse,intellij IDEA,android)开发全过程
    浅谈线程同步的几种方法
    KMP算法,这是我看到的最简单的最好理解的KMP算法
    常用基础算法C++实现
    堆内存和栈内存详解(转载)
    数据结构=。= 链表
    倒排索引--资料1
    倒排索引简单理解
    简单理解Socket
    8.结构体的使用 2015.12.3
  • 原文地址:https://www.cnblogs.com/lj8023wh/p/10605329.html
Copyright © 2011-2022 走看看