#--Name: Requests库详解


0x00 什么是Requests?

    Request是用Python语音编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库.

    比urllib更方便,节省大量的工作,完全满足HTTP测试需求

    总而言之---Python实现的简单易用的HTTP库


0x01 Install It

    pip install requests

    安装完成后,使用下面的命令验证安装是否成功

    import requests


0x02 实例

    

    #实例1:  使用requests的相关方法操作

    import requests


    response = requests.get('https://www.baidu.com') 

    print(type(response))

    print(response.status_code)

    print(type(response.text))

    print(response.cookies)


    #实例2:  各种请求方式

    import requests


    requests.post('http://httpbin.org/post')

    requests.put('http://httpbin.org/put')

    requests.delete('http://httpbin.org/delete')

    requests.get('http://httpbin.org/get')

    requests.head('http://httpbin.org/get')

    requests.options('http://httpbin.org/get')


0x03 请求部分

    #基本GET请求

    #基本写法:


    import requests


    response = requests.get('http://httpbin.org/get')

    print(requests.text)


    #带参数的GET请求

    import requests


    response = requests.get('http://httpbin.org/get?name=germey&age=22')

    print(response.text)


    

    #构造GET请求参数

    import requests


    data = {

        'name':'germey',

        'age':22

    }

    response = requests.get('http://httpbin.org/get",params = data)

    print(response.text)


    #解析JSON

    import requests

    import json


    response = requests.get('http://httpbin.org/get')

    print(type(response.text))

    print(response.json())

    print(json.loads(response.text))

    print(type(response.json()))


    #获取二进制数据

    import requests


    response = request.get('https://github.com/favicon.ico')

    print(type(response.text),type(response.content))

    print(response.text)

    print(response.content)


    #获取二进制数据并写入文件

    import requests


    response = request.get('https://github.com/favicon.ico')

    with open('favicon.ico','wb') as f:

        f.write(response.content)

        f.close()



    #添加headers

    #未添加headers,该url返回报错

    import rquests


    response = requests.get('https://www.zhihu.com/explore')

    print(response.text)


    #添加headers

    import requsts


    headers = {

        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'

    }

    response = requests.get('https://www.zhihu.com/explore',headers = headers)

    print(response.text)



    #基本POST请求

    import requests


    data = {'name':'germey','age':'22'}

    response = requests.post('http://httpbin.org/post',data=data)

    print(response.text)


    #带有headers的POST请求

    import requests

    headers = {

        'User-Agent':'Ruby'

    }

    data = {'name':'germey','age':'22'}

    response = requests.post('http://httpbin.org/post',data=data,headers=headers)

    print(response.text)


    #响应部分

    #response 属性


    import requests


    response = requests.get('https://www.baidu.com')

    print(type(response.status_code),response.status_code)

    print(type(response.headers),response.headers)

    print(type(response.cookies),response.cookies)

    print(type(response.url),response.url)

    response = requests.get('http://www.jd.com')

    print(type(response.history),response.history)



    #状态码的判断

    #判断状态码是否是 ok,也就是http 200 ok

    import requests


    response = requests.get('https://www.baidu.com')

    #exit() if not response.status_code == requests.codes.ok  else print('网站正常')

    if not response.status_code ==  requests.codes.ok:

        exit()

    else:

        print('网站正常')



    #判断状态码是否等于200,同上

    import requests


    response = requests.get('http://www.jianshu.com')

    exit() if not response.status_code == 200 else print('Request Successfully')





    #requests 的高(gao)级(ji)操作

    #文件上传

    import reqeusts


    files = {'file':open('favicon.ico','rb)}

    response = requests.post('http://httpbin.org/post',files=files)

    print(response.text)



    #获取Cookie

    import requests


    response = requests.get('https://www.baidu.com')

    print(response.cookies)

    for key,value in response.cookies.items():

        print(key + '=' + value)


    #会话维持  模拟登陆

    #用例1: 未使用Session对象

    import requests


    requests.get('http://httpbin.org/cookies/set/number/123456789')

    response = requests.get('http://httpbin.org/cookies')

    print(response.text)


    #用例2: 使用session对象

    import requests


    s = requests.Session()

    s.get('http://httpbin.org/cookies/set/number/123456789')

    response = requests.get('http://httpbin.org/cookies')

    print(response.text)



    #证书验证

    #requests打开https的时候,判断证书是否是合法的,12306的证书是非法的,那么

    #我们使用常规的方法是无法访问的

    import requests


    response = requests.get('https://www.12306.cn/')

    print(response.status_code)


    #用例2: 不使用证书访问https

    import requests


    response = requests.get('https://12306.cn',verify=False)

    print(response.status_code)


    #用例3:  使用证书 使用指定证书和密钥

    import requests


    response = requests.get('https://12306.cn',cert=('/path/server.crt','/path/key'))

    print(requests.status_code)



    #有关于代理的使用

    #单一代理设置

    import requests


    proxies = {

        'http':'http://127.0.0.1:9991',

        'https':'https://127.0.0.1:9991'

    }


    response = requests.get('https://www.taobao.com',proxies=proxies)

    print(response.status_code)


    #用例2: 带用户名密码的代理设置

    import requests


    proxies={

        'http':'http://user:password@127.0.0.1:9991'

    }

    response = requests.get('https://www.taobao.com',proxies=proxies)

    print(response.status_code)


    #用例3: 使用Socks代理

    pip install requests[socks]

    import requests


    proxies = {

        'http':'socks5://127.0.0.1:×××',

        'https':'socks5://127.0.0.1:×××'

    }

    response = requests.get('https://www.taobao.com',proxies=proxies)

    print(response.status_code)




    #超时设置

    import requests

    response = requests.get('https://www.taobao.com',timeout = 1)

    print(response.status_code)




    #认证设置

    #对于认证页面,需要使用认证方式

    #用例1: 未设置认证值

    import requests

    from requests.auth import HTTPBasicAuth


    r = requests.get('http://127.0.0.1:1111')

    print(r.status_code)



    #用例2: 设置认证值

    import requests

    from requests.auth import HTTPDigestAuth


    r = requests.get('http://192.168.16.1',auth=HTTPDigestAuth('aaa','123456'))

    print(r.status_code)




    #异常处理

    import requests

    from requests.exceptions import ReadTimeout,HTTPError,RequestException

    try:

        response = requests.get('http://httpbin.org/get',timeout = 0.1)

        print(response.status_code)

    except ReadTimeout:

        print('Time Out!!!')

    except HTTPError:

        print('HTTP Error!!!')

    except RequestException:

        print('Error!!!')