【Python接口测试】03_Urllib接口请求

python 的自带库 urllib

import urllib.request
import urllib.parse

get_url = 'http://www.baidu.com'

def get_request():
    ru = urllib.request.Request(url=url)
    print('ru: ', ru)  #  对象

    ru_response = urllib.request.urlopen(ru)
    print('ru_response: ', ru_response)  # 

    print('getcode: ', ru_response.getcode())
    print('headers: ', ru_response.headers)
# get_request()


post_url = 'http://www.tuling123.com/openapi/api'
post_data = {
    'key': "Your",
    'info': '你好'
}

def post_request():
    data = urllib.parse.urlencode(post_data).encode('utf-8')
    rp = urllib.request.Request(post_url, data)
    rp_response = urllib.request.urlopen(rp)

    print('getcode: ', rp_response.getcode())
    print('msg', rp_response.msg)
    print('read', rp_response.read())
# post_request()

你可能感兴趣的:(Python接口自动化测试)