zoukankan      html  css  js  c++  java
  • python3接口测试(requests库)

    一、一般概念

    1.导入第三方库

    import requests

    2.发送get请求
    #userURL为客户端访问的URL地址
    myResponse = requests.get(userURL)

    3.查看返回结果
    #myResponse.header包含内容:{'Server': 'nginx/1.10.1', 'Date': 'Sat, 18 Aug 2018 02:57:28 GMT', 'Content-Type': 'text/html; charset=GBK', 'X-Powered-By': 'PHP/7.0.10', 'Content-Encoding': 'gzip', 'Age': '0', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Via': 'http/1.1 swg.com ("SKG-UCSG")'}

    4.requests函数有几个典型方法

    requests.request()

    requests.get()

    requests.post()

    requests.put()

    requests.delete()

    requests.head()

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    二、应用

    要求:

    1.请求的URL是https,可能会报SSL认证错误;

    2.对请求头(request head)中的元素赋值,例如content-type。

    3.通过代理(proxies)发送请求。

    import requests
    
    #访问https时会报证书错误((Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] ),可以在发送请求时不验证。
    #屏蔽waring信息
    #requests.packages.urllib3.disable_warnings()
    
    #一般的get请求方法
    #myrequest = requests.get(r"https://www.baidu.com",verify=False)
    #print (myrequest.headers)
    
    #准备请求数据
    myUrl = r"https://www.cnblogs.com/mpp0905/p/9264465.html"
    myHeader = {"content-type":"image/jpeg"}
    
    #代理
    myProxies = {'http' : 'http://url:port'}
    
    #发送请求
    myResponse= requests.request("GET",myUrl,headers=myHeader,proxies=myProxy,verify=False)

    #打印请求数据头
    print (myResponse.request.headers) #当返回页面中有中文时,需要对返回页面进行编码 myResponse.encoding = 'utf8' #判断返回页面 print (myResponse.status_code) assert u"页面中应出现的元素" in (myResponse.text),u'不是提示界面。'








  • 相关阅读:
    1260. [CQOI2007]涂色【区间DP】
    2733. [HNOI2012]永无乡【平衡树-splay】
    1087. [SCOI2005]互不侵犯King【状压DP】
    1026. [SCOI2009]windy数【数位DP】
    1066. [SCOI2007]蜥蜴【最大流】
    luogu P2776 [SDOI2007]小组队列
    cogs 717. [SDOI2007] 小组队列
    luogu P1160 队列安排
    2612. [FHZOI 2017]被窃的项链
    codevs 3336 电话网络 (2)
  • 原文地址:https://www.cnblogs.com/wanwanmom/p/9498150.html
Copyright © 2011-2022 走看看