python利用requests模拟ht

发布时间:2019-09-08 09:11:38编辑:auto阅读(1545)


    一、通过requests发送请求

    之前一直使用urllib以及urllib2模拟http请求发送,在实际场景中,我们需要造自己定义好的header、body等等,使用urllib很麻烦,很偶然的机会,接触到了requests,可以通过发送xml、简单易用,直接上代码:

    requests 
    
    connected():
        
        #通过形参传入url以及请求参数,get方法发送请求
        (param):
            get = requests.get(==param)
            
            #关闭连接
            get.close()
            
            #返回response
            get.text
    
        
        #通过形参传入url,测试数据,请求头,post方法发送请求
        (dataheader):
            post = requests.post(=data=header)
            
            #关闭连接
            post.close()
            
            #返回respone
            post.text


    二、获得session,并把session插入到header里发送给服务器

    from com.utils.ConnUrl import ConnUrl
    import requests
    
    class getheader():
    
        @staticmethod
        def session():
        
        #封装测试数据
            data = 'test data'
            url = ConnUrl.getUrl ()
            url = url+'200000'
        #设置header
            header = {'content-type':'application/octet-stream'}
        #post请求
            post = requests.post(url, data=data, headers=header)
        #通过split函数切割返回数据,获得session
            jsessionid = post.headers['Set-Cookie'].split(';')[0]
            return jsessionid

关键字

上一篇: python 购物流程脚本

下一篇: python发送短信